android开发tablayout怎样实现点击tab刷新

2025-04-27 07:41:58
推荐回答(1个)
回答1:

重新设置点击事件
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);

for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
tab.setCustomView(pagerAdapter.getTabView(i));
if (tab.getCustomView() != null) {
View tabView = (View) tab.getCustomView().getParent();
tabView.setTag(i);
tabView.setOnClickListener(mTabOnClickListener);
}
}
}
viewPager.setCurrentItem(1);

处理点击事件
private View.OnClickListener mTabOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = (int) view.getTag();
if (pos == 0) {
Toast.makeText(BottomTabActivity.this, "您还没有登录", Toast.LENGTH_SHORT).show();
//TODO 跳转到登录界面
} else {
TabLayout.Tab tab = tabLayout.getTabAt(pos);
if (tab != null) {
tab.select();
}
}
}
};