eshteghel company
eshteghel company

Reputation: 471

Layout scrolling up when view pager moves to new page

I have a layout that contains a viewpager (on the top of the layout) and a listview(Below the pager), and I'm moving to new pages every 3 seconds using the Runnable class, the problem is whenever the pager moves to newpage the layout scrolls up so that I can see the pager. I want to prevent scrolling up and keep the position wherever I'm.

pageAdapter = new PagerAdapterCustom(getSupportFragmentManager(),
                getApplicationContext(), pics, true, false);
        pager.setAdapter(pageAdapter);
        mIndicator = (CirclePageIndicator) findViewById(R.id.indicatorDetailsNews);

        mIndicator.setViewPager(pager);
        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
                currentPage = arg0;
                //mIndicator.setCurrentItem(arg0);
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
                //mIndicator.setCurrentItem(arg0);
            }
        });

        final Handler handler = new Handler();

        final Runnable Update = new Runnable() {

            public void run() {
                if (currentPage == NUM_PAGES - 1) {
                    currentPage = -1;
                }
                pager.setCurrentItem(currentPage++, true);
                mIndicator.setCurrentItem(currentPage++);
                pager.setScrollBarFadeDuration(R.styleable.CirclePageIndicator_fillColor);
                // pager.setScrollDurationFactor(4);
            }
        };

        swipeTimer = new Timer();
        swipeTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                handler.post(Update);
            }
        }, 1000, 3000);

Upvotes: 0

Views: 1114

Answers (2)

samugi
samugi

Reputation: 395

Knossos answer is the way, you should manage this from your container Activity,

Let's suppose you're using a RecyclerView, whenever you detect your RecyclerView is scrolled, warn your Activity, you can do something like:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            scrolledValue = dy;

        }
    });

and send right back the scrolledValue to the ContainerActivity. ContanierActivity will then notify all the other Fragments about the change and scroll their RecyclerViews by scrolledValue, whether it's a positive or a negative value.

For this I think you can use smoothScrollBy(int dx, int dy), but I didn't try it yet. See the documentation

Upvotes: 1

Knossos
Knossos

Reputation: 16068

The issue is that the Fragment inside your PagerAdapterCustom have no idea of the scrolling state of its' sibling Fragments.

So when you scroll one, the others will of course still be at the top.

To fix this, you need to do two things.

  1. When scrolling inside a Fragment is detected, you report that scrolling back to the owner of the PagerAdapterCustom.
  2. When you switch to a new Fragment, you report the current scrolling state to the new Fragment.

Upvotes: 0

Related Questions