TextView inside ViewPager intercepts touch events

前端 未结 2 549
我寻月下人不归
我寻月下人不归 2021-02-04 07:03

I have a ViewPager that contains two fragments. In any of the fragments I can touch a place, swipe to switch to another fragment. One of the fragments

相关标签:
2条回答
  • 2021-02-04 07:41

    This thing bothered me too, but I've managed to find the answer.

    Basically, the case is: if the view can scroll horizontally, it intercepts the horizontal motion event and ViewPager is not able to process it anymore.

    Since API Level 14 TextViews have android:scrollHorizontally property (and setHorizontallyScrolling(boolean) method), which, if set to true, causes the TextView to intercept horizontal scroll motion events.

    You may set it to false either in XML or right in the code, but watch out: android:singleLine property forces android:scrollHorizontally to be set to true! Very tricky point! But fortunately, you usually able to safely replace single line property with android:maxLines="1" and necessary ellipsize value.

    Good luck!

    0 讨论(0)
  • 2021-02-04 07:56

    you can override onTouchEvent() of the TextView:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            return false;
        }
    
        return super.onTouchEvent(event);
    }
    
    0 讨论(0)
提交回复
热议问题