gpt4 book ai didi

android - 在 BottomNavigationView 中按下后退按钮时按钮没有改变

转载 作者:行者123 更新时间:2023-11-29 16:33:02 26 4
gpt4 key购买 nike

我在底部导航栏的 dashbordActivity 中使用了三个 fragment 。在后退按钮上正确按下前一个 fragment View 但底部导航按钮没有改变。

在我从底部导航中选择个人资料并按后退按钮后,第一张图片中的主页 fragment 已被选中。 fragment 发生变化,但底部导航项没有变化。 enter image description here

这是我的底部导航 View .....

  private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
case R.id.navigation_home:
setTitle("Home");
fragment = new FirstFragment();
loadFragment(fragment);
return true;
case R.id.navigation_favorite:

setTitle("favourite");
fragment = new MyFavoriteAdsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:

setTitle("Profile");
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
case R.id.navigation_myads:
setTitle("Chat");
fragment = new MyAdsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_chat:
// mTextMessage.setText(R.string.title_chat);
setTitle("Chat");
fragment = new TabFragmentChatUserList();
loadFragment(fragment);
/* Intent i=new Intent(getApplicationContext(),Chat.class);
startActivity(i);*/
return true;
}
return false;
}
};


private void loadFragment(Fragment fragment) {
// load fragment
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack("TAG");
transaction.commit();
/* fragmentManager.beginTransaction().replace(R.id.home_container, frag).addToBackStack(null).commit();*/
}

ob back press 我试过了...

   @Override
public void onBackPressed()
{

// if your using fragment then you can do this way
int fragments
=getSupportFragmentManager().getBackStackEntryCount();
if (fragments == 1) {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", null)
.show();


} else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
if(navigation.getSelectedItemId () != R.id.navigation_favorite)
{
navigation.setSelectedItemId(R.id.navigation_favorite);
}
else if(navigation.getSelectedItemId () != R.id.navigation_chat)
{
navigation.setSelectedItemId(R.id.navigation_chat);
}
else if(navigation.getSelectedItemId () != R.id.navigation_profile)
{
navigation.setSelectedItemId(R.id.navigation_profile);
}
else
{
super.onBackPressed();
}

} else {
super.onBackPressed();
}
}

最佳答案

您需要检查您的后台堆栈并将监听器添加到您的 fragment 管理器,之后根据您的 fragment 您需要根据您的 fragment 设置特定的按钮检查。

fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
// If the stack decreases it means I clicked the back button
if( fragmentManager.getBackStackEntryCount() <= count){
//check your position based on selected fragment and set it accordingly.
navigation.getMenu().getItem(your_pos).setChecked(true);
}
}
});

关于android - 在 BottomNavigationView 中按下后退按钮时按钮没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53813235/

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