How do disable paging by swiping with finger in Vi

2018-12-31 04:42发布

I have ViewPager and below it I have 10 buttons. By clicking on button, for example #4, the pager goes immediately to page #4 by mPager.setCurrentItem(3);. But, I want to disable the paging by swiping with finger horizontally. Thus, the paging is done ONLY by clicking on the buttons. So, how I can disable the swiping with finger?

17条回答
十年一品温如言
2楼-- · 2018-12-31 05:24

Try overriding and returning true from either onInterceptTouchEvent() and/or onTouchEvent(), which will consume touch events on the pager.

查看更多
心情的温度
3楼-- · 2018-12-31 05:26

None of the above code is working smoothly.i tried this

<HorizontalScrollView
                    android:id="@+id/horizontalScrollView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fillViewport="true">

                    <android.support.v4.view.ViewPager
                        android:id="@+id/pager"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </HorizontalScrollView>
查看更多
路过你的时光
4楼-- · 2018-12-31 05:29

If you are extending from ViewPager, you must also override executeKeyEvent as indicated previously by @araks

@Override
public boolean executeKeyEvent(KeyEvent event)
{
    return isPagingEnabled ? super.executeKeyEvent(event) : false;
}

Because some devices like the Galaxy Tab 4 10' show these buttons where most devices never show them:

Keyboard buttons

查看更多
宁负流年不负卿
5楼-- · 2018-12-31 05:30

I needed to disable swiping on one specific page, and give it a nice rubber-band animation, here's how:

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            if (position == MANDATORY_PAGE_LOCATION && positionOffset > 0.5) {
                mViewPager.setCurrentItem(MANDATORY_PAGE_LOCATION, true);
            }
        }
查看更多
谁念西风独自凉
6楼-- · 2018-12-31 05:31

To disable swipe

mViewPager.beginFakeDrag();

To enable swipe

mViewPager.endFakeDrag();
查看更多
登录 后发表回答