我也将TabLayout与ViewPager一起使用,我也必须听TabReselected事件,但似乎它们是冲突的:
mViewPager.setAdapter(...);
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Toast.makeText(getActivity(), "selected...", Toast.LENGTH_LONG).show();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Toast.makeText(getActivity(), "reselected...", Toast.LENGTH_LONG).show();
}
});
mTabLayout.setupWithViewPager(mViewPager);
似乎当我使用setupWithViewPager(mViewPager)时,之前添加的侦听器将无效。关键是当选项卡为[[reselected]]时我需要做其他事情,但是如何?
最佳答案
//使用runnable,因为它需要花费一些时间来设置viewpager:
viewPager.setAdapter(viewpagerAdapter);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Toast.makeText(MainActivity.this, "selected...", Toast.LENGTH_LONG).show();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Toast.makeText(MainActivity.this, "reselected...", Toast.LENGTH_LONG).show();
}
});
}
});
关于android - TabLayout的setOnTabSelectedListener与它的setupWithViewPager冲突吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36300651/