以一个手势同时移动两个VIewPagers

时间:2014-04-04 11:33:23

标签: android android-viewpager

我需要在一个幻灯片手势中将2个不同的ViewPage中的幻灯片转换到其中任何一个。我怎么能这样做呢?

2 个答案:

答案 0 :(得分:0)

在ViewPager类

中的OnPageChangeListener中添加以下代码
public void onPageSelected(int position)
{
    secondViewPager.setCurrentItem(position, true);
}

答案 1 :(得分:0)

下面的代码将同时滑动ViewPagers,无论大小是否相同

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);
        }
    }
});

对另一个ViewPager使用相同的代码。