gpt4 book ai didi

java - 如何在选项卡式 Activity 中切换回上一个选项卡

转载 作者:行者123 更新时间:2023-11-30 00:14:25 26 4
gpt4 key购买 nike

我有一个选项卡式 Activity 1 到 5。如果我从 3 移动到 5,然后按下后退,我想从 5 切换回 3,即按下后退,我想回到上一个选项卡。请帮忙

最佳答案

使用下面的代码来实现它。(代码也经过测试。)

1.初始化一个整数列表和一个 boolean 值;

private boolean isBackPressed = false;  
private boolean isBackPressedOnce = false;
private List<Integer> tabsInBack = new ArrayList<>();

2.现在你的 onTabSelectedListener 必须是这样的

tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if(!isBackPressed){
tabsInBack.add(tab.getPosition());}
else {
isBackPressed = false;}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

3(i).现在你的 onBackPressed() 函数必须是,(一个选项卡返回然后关闭)

    @Override
public void onBackPressed() {
isBackPressed = true;
if (tabsInBack != null && tabsInBack.size() > 0) {
if (tabs.getSelectedTabPosition() == tabsInBack.get(tabsInBack.size() - 1)) {
tabsInBack.remove(tabsInBack.size() - 1);

}
if (tabsInBack != null && tabsInBack.size() > 0) {
tabs.getTabAt(tabsInBack.get(tabsInBack.size() - 1)).select();
tabsInBack.remove(tabsInBack.size() - 1);

} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}

3(ii)。现在你的 onBackPressed() 函数必须是,(返回所有选定的标签然后关闭)

    @Override
public void onBackPressed() {
isBackPressed = true;
if (tabsInBack != null && tabsInBack.size() > 0) {
if (tabs.getSelectedTabPosition() == tabsInBack.get(tabsInBack.size() - 1)) {
tabsInBack.remove(tabsInBack.size() - 1);

}
if (tabsInBack != null && tabsInBack.size() > 0) {
tabs.getTabAt(tabsInBack.get(tabsInBack.size() - 1)).select();
tabsInBack.remove(tabsInBack.size() - 1);
if(isBackPressedOnce )
{
super.onBackPressed();
}
else
{
isBackPressedOnce = true;
}
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}

让我知道它是否有帮助。

关于java - 如何在选项卡式 Activity 中切换回上一个选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47551172/

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