我有一个视图列表,希望能够在单击视图时触发OnItemClickListener。但同时我也想能够滑动每个视图并执行自定义操作。这意味着我必须在ArrayAdapter中为每个视图创建自己的OnTouchEvent。
有没有办法让它们一起工作,以便我可以轻松地执行自定义操作,如滑动项目和单击项目?
有没有办法让它们一起工作,以便我可以轻松地执行自定义操作,如滑动项目和单击项目?
这与Android最近活动的处理方式非常相似 - 你知道,它们显示了所有最近打开的应用程序列表,可以通过滑动删除或点击打开。请查看他们的代码,我认为你会有一个很好的理解:https://github.com/android/platform_frameworks_base/tree/master/packages/SystemUI/src/com/android/systemui/recent
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
Log.d(TAG, "ontouch: UP");
**// Here you can figure if it was simple item click event.
// We return false only when user touched once on the view.
// this will be handled by onItemClick listener.**
if(lastAction == -1){
lastAction = MotionEvent.ACTION_UP;
view.clearFocus();
return true;
}
return false;
}
else if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
Log.d(TAG, "ontouch: DOWN");
return false;
}
else {
// This is all action events.
lastAction = -1;
return true;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// We come here after onTouch event figured out that its a simple touch event and needs to be handled here.
}