Reputation: 4351
I want to use a ListView
(and have done this successfully before) containing custom Views.
Basically these custom views are vertical sliders, obviously conflicting with the natural behaviour of the ListView
.
I fill it with my custom Views from an Adapter
, and these react to the touches on the Items,
but once I move my finger more than a few pixels, it will scroll, and the custom View will not receive any touch-events anymore.
How can I (nicely) prevent the ListView
from scrolling, when I touch my own components?
Can I somehow disable ListView
Selection, and just forward the touches to the Items, but still use the scrolling behaviour?
Thank you in advance.
Upvotes: 2
Views: 9875
Reputation: 28856
You can intercept all touch events using onInterceptTouchEvent()
in your root layout (one that contains the ListView
, like a FrameLayout
) as found here.
What you do there is capture the motion events (return true when a MotionEvent.ACTION_DOWN
comes in), capture the following event in onTouchEvent()
, decide whether the motion is meant for the list items or the list itself and accordingly dispatch the events.
Don't expect this to work easily. Understanding the flow of motion events and the interaction between onInterceptTouchEvent()
and onTouchEvent()
is challenging and making it work even more so. But I'm confident that this is a feasible way to solve your problem.
Upvotes: 1
Reputation: 135
To prevent the scrolling of listview you can inplement on touch listener as follows
listView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_MOVE)
{
return true;
}
return false;
}
});
hope it will work and if it us useful to you give vote
Upvotes: 8