Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager Flickers on touch inside empty area

  • I have a scenario in view ViewPager is used to display multiple fragments depicting number of columns
  • Now in tablet when there are only two pages / column inside the view pager , there remains empty area in the view pager after the two pages as view pager is occupying full screen of the tablet.
  • When the user touches this empty area the remaining pages start flickering scrolling back and forth in the screen.
  • How to restrict user to touch in this empty area ? I also need to allow user swiping the pages to scroll , how to manage both the scenario.
like image 274
Javal Nanda Avatar asked Sep 25 '13 08:09

Javal Nanda


People also ask

When should I use ViewPager?

ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.

How do I know if my ViewPager is scrolling left or right Android?

You save the current page of the ViewPager in OnPageSelected() and compare it to the position parameter of OnPageScrolled() . If the current page is less than or equal to the position , you are scrolling to the right, if not, you are scrolling to the left.

What is AndroidX ViewPager widget ViewPager?

A viewpager with touch and key event handling disabled by default. Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

How do I use ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


1 Answers

I just had this same scenario. My solution was to consume the touch event when there are less pages displaying than required to fill the viewpager

viewPager.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            requestDisallowInterceptTouchEvent(true); // not sure if this is required
            PagerAdapter adapter = viewPager.getAdapter();
            // consume the move event if we have only one page full - removes flickering artifact
            // getNumberOfPagesOnScreen() is a mehtod we have to get the number of pages we are going to display. ymmv
            if (adapter.getCount() <= adapter.getNumberOfPagesOnScreen() && event.getAction() == MotionEvent.ACTION_MOVE) {
                return true;
            } else {
                return false;
            }
        }
    });
like image 188
Darren Avatar answered Sep 28 '22 11:09

Darren