gpt4 book ai didi

android - 将导航 View 上的项目设置为从 fragment 中选择

转载 作者:行者123 更新时间:2023-12-04 02:58:01 25 4
gpt4 key购买 nike

当移动到不同的 fragment 时,我希望突出显示导航菜单中的相应项目。这应该从 fragment 本身完成。以下是 fragment 中的代码:

  saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispName = dispNameET.getText().toString();
myRef.setText(dispName);

//going to another fragment
Fragment fragment = new ListFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();


}
});

在此之后,导航 View 将错误的项目突出显示。请帮忙

最佳答案

你的代码应该是这样的

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispName = dispNameET.getText().toString();
myRef.setText(dispName);

//going to another fragment
Fragment fragment = new ListFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();
NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id./*id of nav bar*/);
navigationView.setCheckedItem(R.id./*id of menu item to be highlighted*/);
}
});

关于android - 将导航 View 上的项目设置为从 fragment 中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51966935/

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