Reputation: 918
I have a container View
that should process touches. It has Listview
with overrided onTouch()
, which returns false
(it will return true
, when container is expanded by swipe). When Listview
touch occurs it passes to container View
and everything is ok. But I need to add click handling for items of Listview
.
When I set onClickListener
for item, I see in logs it is no MotionEvent
passed to container, it is stucked in ListView
onTouch()
(which return false
and does nothing). I'm trying to set onTouchListener
for item like this:
viewHolder.itemLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true;
case MotionEvent.ACTION_MOVE:
return false;
case MotionEvent.ACTION_UP:
Log.d(TAG, "something like click detected");
return true;
}
return false;
}
});
But result is the same. Can someone explain why my container does not receive touch event even ListView
returns false
for its overrided onTouch()
?
Upvotes: 0
Views: 908
Reputation: 666
I had the same problem about 2 weeks ago. Returning false from listview's onTouch method did not pass event to parent container. I ended up doing this:
1. You need custom layout for your container View
. Then in that view, override onInterceptTouchEvent, for example like this:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
previousX_actionDOWN = ev.getRawX();
isOnClick = true;
timePressed = System.currentTimeMillis();
return false;
case MotionEvent.ACTION_MOVE:
if (isOnClick && (Math.abs(previousX_actionDOWN - ev.getRawX()) > SCROLL_THRESHOLD)) {
return true;
} else {
return false;
}
case MotionEvent.ACTION_UP:
if (!wasDragged) {
return false;
}
else {
return true;
}
}
return false;
}
I also have complicated onTouch method in that view but it's not worth pasting. Point is that your parent view should only intercept touch event if some threshold was passed while ACTION_MOVE - well at least that's what I wanted to achieve. Then in your listview you just implement standard onItemClickListener instead of onTouch.
Upvotes: 1
Reputation: 226
If you wish to handle the clicks of ListView items : use OnItemClickListener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//parent.getItemAtPosition(position) returns the value of item clicked.. use it to do whatever you wish to do
}
});
Upvotes: 1