带有 OnTouchListener 的 Gridview 滚动
Gridview Scroll with OnTouchListener
基本上,我想快速 select 项目以及滚动 Gridview。在 OnItemClickListener 中,滚动工作正常但速度不快 select 像 OnTouchListener 这样的多个项目。
我的代码:
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;
}
});
现在它的滚动和添加项目也是如此。我如何在 ACTION_MOVE(when scroll)
之后停止 ACTION_UP(selection Item)
事件?
我将此代码应用于 OntouchListener 上的滚动 gridview 并防止在 select 项目上滚动。在我身边工作得很好。如果有人有更好的解决方案,请提出来。
我的代码:
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;
}
});
基本上,我想快速 select 项目以及滚动 Gridview。在 OnItemClickListener 中,滚动工作正常但速度不快 select 像 OnTouchListener 这样的多个项目。
我的代码:
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;
}
});
现在它的滚动和添加项目也是如此。我如何在 ACTION_MOVE(when scroll)
之后停止 ACTION_UP(selection Item)
事件?
我将此代码应用于 OntouchListener 上的滚动 gridview 并防止在 select 项目上滚动。在我身边工作得很好。如果有人有更好的解决方案,请提出来。
我的代码:
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;
}
});