Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an indication that the user is trying to over scroll in a ViewPager

I want to start a new activity once a user tries to scroll when he is on the last fragment(when the bounce effect happens). I am using ViewPagerIndicator

The code below launches activity once the last fragment is visible.

Any idea?

 CirclePageIndicator titleIndicator = (CirclePageIndicator)findViewById(R.id.titles);
    titleIndicator.setViewPager(pager);
    titleIndicator.(new );
    titleIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
                selectedIndex = position;

        }

        @Override
        public void onPageScrolled(int position, float arg1, int arg2) {


        }

        @Override
        public void onPageScrollStateChanged(int scroll) {
            if(selectedIndex == adapter.getCount()-1)
            {
               launchactivity)(;
            }


        }


    });
like image 658
user1163234 Avatar asked Oct 20 '22 21:10

user1163234


1 Answers

We are interested only in cases where we have overscroll, that is 0 and mPager.getAdapter().getCount() - 1 When the state of the scroller is ViewPager.SCROLL_STATE_DRAGGING means that finger of the user is still on the screen and dragging the view. The condition below needs consecutive 0 positionOffset to decide whether it is entering in the overscroll mode.

 titleIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                private int mScrollState = -1;
                private float mPreviousPageOffset;

                @Override
                public void onPageScrollStateChanged(int state) {
                    super.onPageScrollStateChanged(state);
                    mScrollState = state;
                    if(mScrollState == ViewPager.SCROLL_STATE_IDLE) mPreviousPageOffset = -1;
                }

                @Override
                public void onPageScrolled(int position, float positionOffset,
                        int positionOffsetPixels) {
                    super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                    boolean isLastPosition = position == mPager.getAdapter().getCount() - 1;
                    if(isLastPosition && mScrollState == ViewPager.SCROLL_STATE_DRAGGING && mPreviousPageOffset == 0f && positionOffset == 0f){
                          //launchActivity
                    }
                    mPreviousPageOffset = positionOffset;
                }
            });

If the user releases the finger, the positionOffset will still go 0 but the scroller state will be ViewPager.SCROLL_STATE_SETTLING or the we will get 0 for positionOffset only once.

like image 91
Nikola Despotoski Avatar answered Oct 22 '22 10:10

Nikola Despotoski