问题
I have an EditText (vertically scrollable) inside one of the fragments in a ViewPager (horizontally swipable).
By default, touch events inside the EditText can swipe the ViewPager but not scroll the EditText. By using the code below (which I don't really understand), touch events inside the EditText can scroll the EditText but not swipe the ViewPager:
editText.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
v.getParent().requestDisallowInterceptTouchEvent(! ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP));
return false;
}
});
How can I simultaneously allow swiping of the ViewPager and scrolling of the EditText?
回答1:
You can handle this according to the solution given here.
pager.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
editText.getParent().requestDisallowInterceptTouchEvent(false);
// We will have to follow above for all scrollable contents
return false;
}
});
For the EditText, you will have to add this snippet.
editText.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
// this will disallow the touch request for parent scroll on touch of child view
p_v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Hope it helps...
来源:https://stackoverflow.com/questions/29471842/allow-scrolling-edittext-and-swiping-viewpager