Ad
Gridview Scroll With OnTouchListener
Basically, I want to fast select item as well as scroll Gridview. In OnItemClickListener scroll working fine but its not fast select multiple item like OnTouchListener.
My code:
gridView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent me) {
int action = me.getActionMasked();
float currentXPosition = me.getX();
float currentYPosition = me.getY();
int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
switch (action) {
case (MotionEvent.ACTION_MOVE):
break;
case (MotionEvent.ACTION_UP):
if (position != -1) {
//Here my logic to add item in basket list
return true;
}
}
return false;
}
});
Right now its scroll and add item as well. How I stop ACTION_UP(selection Item)
event after ACTION_MOVE(when scroll)
?
Ad
Answer
I was applying this code for scroll gridview on OntouchListener and prevent scroll on select item. And work fine in my side. If anyone have better solution bring it on.
My Code:
private int moveCount=0;
private boolean ignore = false;
gridView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent me) {
v.getParent().requestDisallowInterceptTouchEvent(true);
int action = me.getActionMasked(); // MotionEvent types such as ACTION_UP, ACTION_DOWN
float currentXPosition = me.getX();
float currentYPosition = me.getY();
int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
if (ignore && action == MotionEvent.ACTION_UP)
return false;
switch (action) {
case (MotionEvent.ACTION_MOVE):
moveCount++;
Log.d(DEBUG_TAG, "Action was MOVE " + position);
if(moveCount>3) {
ignore = true;
}
break;
case (MotionEvent.ACTION_UP):
addItem(position);
Log.d(DEBUG_TAG, "Action was UP " + position);
return true;
case (MotionEvent.ACTION_DOWN):
Log.d(DEBUG_TAG, "Action was DOWN " + position);
moveCount=0;
ignore = false;
return true;
case (MotionEvent.ACTION_CANCEL):
addItem(position);
moveCount=0;
ignore = false;
gridView.setFocusable(true);
Log.d(DEBUG_TAG, "Action was CANCEL " + position);
return true;
case (MotionEvent.ACTION_OUTSIDE):
Log.d(DEBUG_TAG, "Movement occurred outside bounds " +
"of current screen element " + position);
return true;
}
Log.d("clickTouch=", "" + position);
return false;
}
});
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad