gpt4 book ai didi

android - 如何为 Android BottomNavigation 实现正确的后退导航

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:50 25 4
gpt4 key购买 nike

我一整天都在寻找答案,但没有找到适合这个问题的任何解决方案。

我正在寻找一种方法来创建类似于 Instagram 或 PlayKiosk 应用的 BottomNavigation 用法。 fragment 应该只添加到返回堆栈一次。当按下后退按钮时,我希望应用程序跳回到访问过的最后一个 Fragment 和 BottomNavigation 按钮以适应该 Fragment。

目前我使用以下代码:

//BottomNavigationListener
private BottomNavigationView.OnNavigationItemSelectedListener buttonNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (item.getItemId()) {
case R.id.navigation_game:
currentFragment = GameFragment.newInstance();
fragmentManager.beginTransaction()
.addToBackStack(TAG_FRAGMENT_GAME)
.replace(R.id.content_frame, currentFragment, TAG_FRAGMENT_GAME)
.commit();
break;
case R.id.navigation_tournament:
currentFragment = TournamentFragment.newInstance();
fragmentManager.beginTransaction()
.addToBackStack(TAG_FRAGMENT_TOURNAMENT)
.replace(R.id.content_frame, currentFragment, TAG_FRAGMENT_TOURNAMENT)
.commit();
break;
case R.id.navigation_history:
break;
}
return true;
}

};

但这会导致问题,我可以按 BottomNavigation 的按钮几次,每次点击都会实例化一个新的 Fragment。此外,BottomNavigation 按钮未根据 Fragments 设置。

我找到了这个答案,但没有成功 Prevent The Same Fragment From Stacking More Than Once ( addToBackStack)

最佳答案

您可以使用以下结构来拥有与 Instagram 相同的工作逻辑。

首先创建一个自定义的有限唯一队列类。它只包含最后 N 个项目,其中 N 是传递给其构造函数的限制数(或最大项目数)。此外,这种类型的队列只保留一个类的一个实例。如果类A的一个实例已经在队列中,并且要将类A的另一个实例添加到队列中,则先删除前一个实例,然后插入新对象。

public class LimitedUniqueQueue<E> extends LinkedList<E> {

private int limit;

public LimitedUniqueQueue(int limit) {
this.limit = limit;
}

@Override
public boolean add(E o) {
// For uniqueness
for (int i = 0; i < super.size(); i++) {
E item = super.get(i);
if (item.getClass() == o.getClass()) {
super.remove(i);
break;
}
}
boolean added = super.add(o);
// For size limit
while (added && size() > limit) {
super.remove();
}
return added;
}
}

然后按如下方式更新您的 Activity:

public class MainActivity extends AppCompatActivity {

private BottomNavigationView navigation;
private LimitedUniqueQueue<Fragment> queue;
...
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

Fragment gameFragment;
Fragment tournamentFragment;
Fragment profileFragment;

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_game:
if (gameFragment == null) {
gameFragment = new GameFragment();
}
transaction.replace(R.id.content, gameFragment).commit();
queue.add(gameFragment);
return true;
case R.id.navigation_tournament:
if (tournamentFragment == null) {
tournamentFragment = new TournamentFragment();
}
transaction.replace(R.id.content, tournamentFragment).commit();
queue.add(tournamentFragment);
return true;
case R.id.navigation_profile:
if (profileFragment == null) {
profileFragment = new ProfileFragment();
}
transaction.replace(R.id.content, profileFragment).commit();
queue.add(profileFragment);
return true;
}
return false;
}

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

navigation = (BottomNavigationView) findViewById(R.id.navigation);
queue = new LimitedUniqueQueue<>(navigation.getMenu().size());

navigation.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.navigation_game);
...
}

@Override
public void onBackPressed() {
if (queue.size() > 1) {
queue.removeLast();
Fragment previousFragment = queue.getLast();
if (previousFragment instanceof GameFragment) {
navigation.setSelectedItemId(R.id.navigation_game);
} else if (previousFragment instanceof TournamentFragment) {
navigation.setSelectedItemId(R.id.navigation_tournament);
} else if (previousFragment instanceof ProfileFragment) {
navigation.setSelectedItemId(R.id.navigation_profile);
}
} else {
super.onBackPressed();
}
}

...
}

关于android - 如何为 Android BottomNavigation 实现正确的后退导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46549181/

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