Reputation: 187
In my app i did use scroll inside a Scrollview layout(root layout).when i did that the child scroll stopped scrolling.For this i found a solution code
childlistview.setOnTouchListener(new ListView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
v.onTouchEvent(event);
return false;
}
});
It solved the problem.But i can't understand this code.Can anyone help?
Upvotes: 1
Views: 696
Reputation: 16729
In your case, Scrollview
is parent whereas ListView
is its child.
So, in normal case when you try to scroll, the ScrollView
will intercept that event and the scroll of ScrollView
will occur. It will not reach the ListView
within as it is already handlled by the parent ScrollView
.
However, when you set a TouchListener
to the child ListView
and override its the onTouch()
as you have done, a different behaviour is observed.
case MotionEvent.ACTION_DOWN:
This event happens when the first finger is pressed onto the screen.
v.getParent().requestDisallowInterceptTouchEvent(true);
This will prevent the parent from intercepting the touch event, so that the child can handle the event appropriately.
case MotionEvent.ACTION_UP:
This event fires when all fingers are off the screen.
v.getParent().requestDisallowInterceptTouchEvent(false);
This will allow the parent to intercept the touch events thereafter, for rest of the screen leaving the childview.
Hope it helps you.
Upvotes: 2
Reputation: 8251
v.getParent().requestDisallowInterceptTouchEvent(true);
It simply disable the parent touch of the child View
. In your case it disable the parent( ScrollView
) touch of child view (v
).
Again if you want to enable the parent touch, simply pass false
to the same method.
Hope it clears your doubt.
Upvotes: 0