Allow scrolling EditText and swiping ViewPager

烂漫一生 提交于 2019-12-11 11:19:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!