未触发SimpleOnPageListener

时间:2015-08-14 19:00:24

标签: android android-viewpager

我有这堂课:

ViewPagerAdapter adapter =  new ViewPagerAdapter(getSupportFragmentManager(), getTabTitles(), getTabFragments());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
SlidingTabLayout tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly();

pager.setOnPageChangeListener(new PageChangeListener());
tabs.setViewPager(pager);
pager.setCurrentItem(getDefaultTabIndex());

我在这里使用它:

@Override
public void onPageSelected(int position) {
    Toast.makeText(PocketApplication.getContext(), "changed" + position, Toast.LENGTH_LONG).show();
}

我可以在标签之间切换,并且片段加载正常。

但是,我没有看到我的Toast,而且我在PageChangeListener中设置了断点并且没有被触发。

为什么没有被触发?我希望在页面更改时收到通知。

我已尝试实施

class Dispatcher<TPayload> {
  _callbacks: {[key: DispatchToken]: (payload: TPayload) => void};

也是,没有Toast。

1 个答案:

答案 0 :(得分:0)

您需要为setOnPageChangeListener而不是viewpager设置SlidingTabLayout

pager.setOnPageChangeListener(new PageChangeListener());更改为tabs.setOnPageChangeListener(new PageChangeListener());

SlidingTabLayout

中记录了这一点
 /**
     * Set the {@link ViewPager.OnPageChangeListener}. When using {@link SlidingTabLayout} you are
     * required to set any {@link ViewPager.OnPageChangeListener} through this method. This is so
     * that the layout can update it's scroll position correctly.
     *
     * @see ViewPager#setOnPageChangeListener(ViewPager.OnPageChangeListener)
     */
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
        mViewPagerPageChangeListener = listener;
    }