gpt4 book ai didi

android - TabLayout 的setOnTabSelectedListener 和它的setupWithViewPager 有冲突吗?

转载 作者:行者123 更新时间:2023-11-30 01:27:04 24 4
gpt4 key购买 nike

我用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)时,之前添加的Listener会失效。关键是当选项卡 [[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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com