简体   繁体   English

允许滚动EditText和滑动ViewPager

[英]Allow scrolling EditText and swiping ViewPager

I have an EditText (vertically scrollable) inside one of the fragments in a ViewPager (horizontally swipable). 我在ViewPager(水平可滑动)的片段之一内有一个EditText(可垂直滚动)。

By default, touch events inside the EditText can swipe the ViewPager but not scroll the EditText. 默认情况下,EditText内部的触摸事件可以滑动ViewPager,但不能滚动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内部的触摸事件可以滚动EditText,但不能滑动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? 如何同时允许ViewPager的滑动和EditText的滚动?

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,您必须添加此代码段。

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... 希望能帮助到你...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM