简体   繁体   中英

Scroll two view pagers at same time

I have two view pagers in a layout. One at the top and other at the bottom.

When I scroll one view pager, the other view pager should scroll too. But my problem , when top view pager is scrolled, only the top view pager is scrolled, bottom view pager doesn't scroll.

My code :

        topViewPager.setAdapter(topViewPagerAdapter);
        bottomViewPager.setAdapter(bottomPagerAdapet);
        tabLayout.setViewPager(topViewPager);
        tabLayout.setViewPager(bottomViewPager);

You can use a listener:

mViewPager1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
        Log.d(LOG_TAG, "Page scrolled to " + position);

        mViewPager2.setCurrentItem(position)
    }

    @Override
    public void onPageScrollStateChanged(int state) {}

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
});

I suppose the TabLayout should adjust by itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM