我有一个按钮可以让我更改片段,它可以成功完成此操作。我打开了新片段,但底部的导航菜单并未更改其样式。它是简单的底部导航菜单,因此,当您选择片段时,当前正在查看的菜单将使该片段底部的文本变大,图标也变大。 这就是这里的问题,它不会更改菜单。
goButton = (ImageButton) SOMEView.findViewById(R.id.go_button);
goButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Started");
AppCompatActivity mainActivity = new MainActivity();
((MainActivity) mainActivity).bottomNavigation.findViewById(R.id.nav_info).callOnClick();
Fragment nextFragment = new INFOFragment("second");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, nextFragment );
transaction.addToBackStack(null);
transaction.commit();
}
});
INFOFragment:
public class INFOFragment extends Fragment {
public String state;
@SuppressLint("ValidFragment")
public INFOFragment(String state) {
this.state = state;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(state.equals("first")) {
System.out.println("info");
return inflater.inflate(R.layout.fragment_info, container, false);
}
else {
System.out.println("second state started");
return inflater.inflate(R.layout.fragment_another_view, container, false);
}
}}
MainActivity:
public class MainActivity extends AppCompatActivity {
public BottomNavigationView bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigation = findViewById(R.id.bnavigation);
bottomNavigation.setOnNavigationItemSelectedListener(navigationListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new INFOFragment("first")).commit();
bottomNavigation.setSelectedItemId(R.id.nav_info);
}
private BottomNavigationView.OnNavigationItemSelectedListener navigationListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch(item.getItemId()) {
case R.id.nav_another_view:
System.out.println("vid ovo");
selectedFragment = new ANOTHERVIEWFragment();
break;
case R.id.nav_info:
System.out.println("v2id ovo");
selectedFragment = new INFOFragment("prvi");
break;
case R.id.nav_some_view:
selectedFragment = new SOMEFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
return true;
}
};}
答案 0 :(得分:1)
第一步:像这样在MainActivity上创建一个常量
int prev_nav_item = 0;
第2步:
private BottomNavigationView.OnNavigationItemSelectedListener navigationListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch(item.getItemId()) {
case R.id.nav_another_view:
System.out.println("vid ovo");
selectedFragment = new ANOTHERVIEWFragment();
prev_nav_item = 0;
break;
case R.id.nav_info:
System.out.println("v2id ovo");
selectedFragment = new INFOFragment("prvi");
prev_nav_item = 1;
break;
case R.id.nav_some_view:
selectedFragment = new SOMEFragment();
prev_nav_item = 2;
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
return true;
}
};}
第3步:在主要活动中更改底部菜单的位置的功能
public void selectBottomMenu(final int position) {
Handler uiHandler = new Handler();
uiHandler.post(new Runnable() {
@Override
public void run() {
bottomNavigation.setCurrentItem(position);
}
});
}
第3步:在onResume()中像这样更新每个片段的底部菜单位置
@Override
public void onResume() {
super.onResume();
((MainActivity) mActivity).selectBottomMenu(0); //change value depending on your bottom menu position
}