Sur*_*jid 44
没有创建新的自定义ListView的另一个选项是附加onTouchListener到ListView,onTouch()如果运动事件操作是,则在回调中返回true ACTION_MOVE.
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
Run Code Online (Sandbox Code Playgroud)
Poi*_*ull 42
在CustomListView中:
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(ev.getAction()==MotionEvent.ACTION_MOVE)
return true;
return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)
然后ListView将对点击作出反应,但不会改变滚动位置.
小智 29
使用listview.setEnabled(false)禁用listview滚动
注意:这也会禁用行选择
做你的 CustomListView
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(needToStop){
return false;}
return super.onInterceptTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)
对false孩子的将处理触摸事件,确保你把你的if condition检查需要滚动与否.
如果您有一个绑定到列表项的事件,则使用任何这些解决方案拖动列表仍将触发该事件.您希望使用以下方法来考虑用户期望通过拖离所选项目来取消事件(改编自Pointer Null的答案):
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
super.dispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
setPressed(false);
invalidate();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)
提供完整的自定义视图类:https://gist.github.com/danosipov/6498490
对我来说最好的答案是丹·奥西波夫的一个人.我会像这样改进它.(触发CANCEL动作事件而不是手动擦除按下状态).
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) != mPosition) {
// Clear pressed state, cancel the action
ev.setAction(MotionEvent.ACTION_CANCEL);
}
}
return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77041 次 |
| 最近记录: |