简体   繁体   中英

Move two VIewPagers simultaneously in one gesture

I need to make a slide transition in 2 distinct ViewPagers in one slide gesture to either of them. How can I go about that?

Add following code in your OnPageChangeListener in ViewPager class

public void onPageSelected(int position)
{
    secondViewPager.setCurrentItem(position, true);
}

Below code will slide both the ViewPagers simultaneously whether there size is same or not :

viewPagerBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    private int scrollState = ViewPager.SCROLL_STATE_IDLE;
    // Indicates that the pager is in an idle, settled state. 
    // The current page is fully in view and no animation is in progress.

    @Override
    public void onPageScrolled(int position, float positionOffset, 
                               int positionOffsetPixels) {
        if (scrollState == ViewPager.SCROLL_STATE_IDLE) {
            return;
        }
        viewPagerTitle.scrollTo(viewPagerBanner.getScrollX()*
                                viewPagerTitle.getWidth()/
                                viewPagerBanner.getWidth(), 0);
        // We are not interested in Y axis position
    }

    @Override
    public void onPageSelected(int position) {}

    @Override
    public void onPageScrollStateChanged(int state) {
        scrollState = state;
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            viewPagerTitle.setCurrentItem(viewPagerBanner.getCurrentItem(), false);
        }
    }
});

Use the same code for the another ViewPager.

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