-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto Inc/Dec on long press #27
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import android.os.Build; | ||
import android.util.AttributeSet; | ||
import android.util.Log; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.LinearLayout; | ||
|
@@ -113,9 +114,85 @@ public void onClick(View mView) { | |
setNumber(String.valueOf(num + 1), true); | ||
} | ||
}); | ||
|
||
subtractBtn.setOnLongClickListener(new OnLongClickListener() { | ||
@Override | ||
public boolean onLongClick(View v) { | ||
dec_pressed = true; | ||
touchUpdateHandler.post( new RptUpdater() ); | ||
return true; | ||
} | ||
}); | ||
addBtn.setOnLongClickListener(new OnLongClickListener() { | ||
@Override | ||
public boolean onLongClick(View v) { | ||
inc_pressed = true; | ||
touchUpdateHandler.post( new RptUpdater() ); | ||
return true; | ||
} | ||
}); | ||
subtractBtn.setOnTouchListener(TouchListener); | ||
addBtn.setOnTouchListener(TouchListener); | ||
a.recycle(); | ||
} | ||
|
||
|
||
//################### Auto Increment on Long Press ######################## | ||
private boolean inc_pressed = false; | ||
private boolean dec_pressed = false; | ||
private int _DELAY = 100; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a good idea to initialize a variable with underscore in Java. Also it's ideal for constants to be declared in a separate class to hold the constants as the changes will be less. |
||
private Handler touchUpdateHandler = new Handler(); | ||
//private boolean mAutoIncrement = false; | ||
//private boolean mAutoDecrement = false; | ||
//public int mValue; | ||
class RptUpdater implements Runnable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a good idea to declare a sub-class within a class to perform an operation. Try to separate the class out and try to pass the variables instead of using global variables |
||
public void run() { | ||
if( inc_pressed ){ | ||
increment(); | ||
touchUpdateHandler.postDelayed( new RptUpdater(), _DELAY ); | ||
} else if( dec_pressed ){ | ||
decrement(); | ||
touchUpdateHandler.postDelayed( new RptUpdater(), _DELAY ); | ||
} | ||
} | ||
} | ||
|
||
|
||
public void decrement(){ | ||
int num = Integer.valueOf(textView.getText().toString()); | ||
setNumber(String.valueOf(num-1), true); | ||
} | ||
|
||
public void increment(){ | ||
int num = Integer.valueOf(textView.getText().toString()); | ||
setNumber(String.valueOf(num+1), true); | ||
} | ||
|
||
private View.OnTouchListener TouchListener = new View.OnTouchListener() { | ||
|
||
@Override | ||
public boolean onTouch(View pView, MotionEvent pEvent) { | ||
pView.onTouchEvent(pEvent); | ||
|
||
if (inc_pressed) { | ||
if( (pEvent.getAction()==MotionEvent.ACTION_UP || pEvent.getAction()==MotionEvent.ACTION_CANCEL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea to use object.equals instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it will be null safe as it is compared to a constant , but still .equals is better idea |
||
&& inc_pressed ){ | ||
inc_pressed = false; | ||
} | ||
} | ||
else if(dec_pressed) | ||
{ | ||
if( (pEvent.getAction()==MotionEvent.ACTION_UP || pEvent.getAction()==MotionEvent.ACTION_CANCEL) | ||
&& dec_pressed ){ | ||
dec_pressed = false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
//############# ENDS - Auto Increment on Long Press ############### | ||
private void callListener(View view) { | ||
if (mListener != null) { | ||
mListener.onClick(view); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Camel Case these variables