无法更改 header 和工具栏中的抽屉图标和标题 android
Unable to change the header and drawer icon and title in toolbar in android
我正在尝试更改一个片段导航中工具栏的标题和抽屉图标
这是我要实现的:
DrawerItem1 -> HomeFragment -> CategoryFragment -> Frag3 -> Frag4
DrawerItem2 -> FavoriteFragment
DrawerItem3 -> Frag6
DrawerItem4 -> Frag7
所以,当我从 HomeFragment 导航到 FavoriteFragment 时,工具栏抽屉图标变为后退图标,标题变为 'Favorite' 在导航中设置。但是,当我从 HomeFragment -> CategoryFragment -> SubCategoryFragment 导航时,我无法更改工具栏抽屉图标到后退图标和工具栏标题到片段标题。
这是我到目前为止的实现。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private DrawerLayout drawer;
private NavController navController;
private NavHostFragment navHostFragment;
private NavigationView navigationView;
public static Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
navHostFragment =
(NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.nav_host_fragment);
navController = navHostFragment.getNavController();
mAppBarConfiguration = new AppBarConfiguration.Builder(
navController.getGraph())
.setDrawerLayout(drawer)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_category, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, mAppBarConfiguration);
}
@Override
public void onBackPressed() {
}
}
HomeFragment.java
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private RecyclerView rv;
private ProgressBar pb;
private DataAdapter adapter;
private List<DataModel> modelList;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
rv = v.findViewById(R.id.rv_home);
pb = v.findViewById(R.id.home_pb);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
modelList = new ArrayList<>();
Log.d("Home Frag", "onViewCreated: called again after back");
rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
rv.setHasFixedSize(true);
pb.setVisibility(View.VISIBLE);
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
homeViewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(),
userListUpdateObserver);
pb.setVisibility(View.GONE);
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i("HomeFragment", "keyCode: " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
Log.i("HomeFragment", "keyCode: " + keyCode);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Logout");
builder.setMessage("Are you sure you want to Logout?");
// add the buttons
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
getActivity().finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
return false;
}
});
}
Observer<List<Category>> userListUpdateObserver = new Observer<List<Category>>() {
@Override
public void onChanged(final List<Category> userArrayList) {
for (int i = 0; i < userArrayList.size(); i++) {
modelList.add(new DataModel(userArrayList.get(i).getTitle(), userArrayList.get(i).getImage(), userArrayList.get(i).getId(), 0));
}
adapter = new DataAdapter(getContext(), modelList, new DataAdapter.RecyclerViewClickListener() {
@Override
public void onClick(View view, int position) {
homeViewModel.selectedId(modelList.get(position).getId());
homeViewModel.selectedString(modelList.get(position).getText());
SubCategoryFragment subCategoryFragment = new SubCategoryFragment();
ManageFragments.replaceFragment((FragmentActivity) getContext(), subCategoryFragment);
}
});
rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
};
}
mobile_navigation.xml
?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.drawerapp.fragment.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
</fragment>
<fragment
android:id="@+id/nav_sub_category"
android:name="com.drawerapp.fragment.sub_category.SubCategoryFragment"
android:label="SubCategoryFragment">
<action
android:id="@+id/action_nav_sub_category_to_nav_home"
app:destination="@id/nav_home"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/nav_gallery"
android:name="com.drawerapp.fragment.gallery.GalleryFragment"
android:label="GalleryFragment">
<action
android:id="@+id/action_nav_gallery_to_nav_sub_category"
app:destination="@id/nav_sub_category"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/nav_fav"
android:name="com.drawerapp.fragment.favorites.FavoritesFragment"
android:label="@string/menu_fav"
tools:layout="@layout/fragment_favorite">
<action
android:id="@+id/action_nav_fav_to_nav_home"
app:destination="@id/nav_home"
app:exitAnim="@anim/fragment_fade_exit"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/nav_feedback"
android:name="com.drawerapp.fragment.feedback.FeedbackFragment"
android:label="@string/menu_feedback"
tools:layout="@layout/fragment_feedback">
<action
android:id="@+id/action_nav_feedback_to_nav_fav"
app:destination="@id/nav_fav"
app:exitAnim="@anim/fragment_fade_exit"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/nav_about_us"
android:name="com.drawerapp.fragment.about_us.AboutUsFragment"
android:label="@string/menu_about_us"
tools:layout="@layout/fragment_about_us">
<action
android:id="@+id/action_nav_about_us_to_nav_feedback"
app:destination="@id/nav_feedback"
app:exitAnim="@anim/fragment_fade_exit"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
您仍在执行 ManageFragments.replaceFragment((FragmentActivity) getContext(), subCategoryFragment);
- 您无法在使用导航时执行 FragmentTransactions。
将该代码替换为调用 navigate(R.id.nav_sub_category)
以导航至您的 SubCategoryFragment
。
我正在尝试更改一个片段导航中工具栏的标题和抽屉图标
这是我要实现的:
DrawerItem1 -> HomeFragment -> CategoryFragment -> Frag3 -> Frag4
DrawerItem2 -> FavoriteFragment
DrawerItem3 -> Frag6
DrawerItem4 -> Frag7
所以,当我从 HomeFragment 导航到 FavoriteFragment 时,工具栏抽屉图标变为后退图标,标题变为 'Favorite' 在导航中设置。但是,当我从 HomeFragment -> CategoryFragment -> SubCategoryFragment 导航时,我无法更改工具栏抽屉图标到后退图标和工具栏标题到片段标题。
这是我到目前为止的实现。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private DrawerLayout drawer;
private NavController navController;
private NavHostFragment navHostFragment;
private NavigationView navigationView;
public static Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
navHostFragment =
(NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.nav_host_fragment);
navController = navHostFragment.getNavController();
mAppBarConfiguration = new AppBarConfiguration.Builder(
navController.getGraph())
.setDrawerLayout(drawer)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_category, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, mAppBarConfiguration);
}
@Override
public void onBackPressed() {
}
}
HomeFragment.java
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private RecyclerView rv;
private ProgressBar pb;
private DataAdapter adapter;
private List<DataModel> modelList;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
rv = v.findViewById(R.id.rv_home);
pb = v.findViewById(R.id.home_pb);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
modelList = new ArrayList<>();
Log.d("Home Frag", "onViewCreated: called again after back");
rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
rv.setHasFixedSize(true);
pb.setVisibility(View.VISIBLE);
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
homeViewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(),
userListUpdateObserver);
pb.setVisibility(View.GONE);
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i("HomeFragment", "keyCode: " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
Log.i("HomeFragment", "keyCode: " + keyCode);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Logout");
builder.setMessage("Are you sure you want to Logout?");
// add the buttons
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
getActivity().finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
return false;
}
});
}
Observer<List<Category>> userListUpdateObserver = new Observer<List<Category>>() {
@Override
public void onChanged(final List<Category> userArrayList) {
for (int i = 0; i < userArrayList.size(); i++) {
modelList.add(new DataModel(userArrayList.get(i).getTitle(), userArrayList.get(i).getImage(), userArrayList.get(i).getId(), 0));
}
adapter = new DataAdapter(getContext(), modelList, new DataAdapter.RecyclerViewClickListener() {
@Override
public void onClick(View view, int position) {
homeViewModel.selectedId(modelList.get(position).getId());
homeViewModel.selectedString(modelList.get(position).getText());
SubCategoryFragment subCategoryFragment = new SubCategoryFragment();
ManageFragments.replaceFragment((FragmentActivity) getContext(), subCategoryFragment);
}
});
rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
};
}
mobile_navigation.xml
?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.drawerapp.fragment.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
</fragment>
<fragment
android:id="@+id/nav_sub_category"
android:name="com.drawerapp.fragment.sub_category.SubCategoryFragment"
android:label="SubCategoryFragment">
<action
android:id="@+id/action_nav_sub_category_to_nav_home"
app:destination="@id/nav_home"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/nav_gallery"
android:name="com.drawerapp.fragment.gallery.GalleryFragment"
android:label="GalleryFragment">
<action
android:id="@+id/action_nav_gallery_to_nav_sub_category"
app:destination="@id/nav_sub_category"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/nav_fav"
android:name="com.drawerapp.fragment.favorites.FavoritesFragment"
android:label="@string/menu_fav"
tools:layout="@layout/fragment_favorite">
<action
android:id="@+id/action_nav_fav_to_nav_home"
app:destination="@id/nav_home"
app:exitAnim="@anim/fragment_fade_exit"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/nav_feedback"
android:name="com.drawerapp.fragment.feedback.FeedbackFragment"
android:label="@string/menu_feedback"
tools:layout="@layout/fragment_feedback">
<action
android:id="@+id/action_nav_feedback_to_nav_fav"
app:destination="@id/nav_fav"
app:exitAnim="@anim/fragment_fade_exit"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/nav_about_us"
android:name="com.drawerapp.fragment.about_us.AboutUsFragment"
android:label="@string/menu_about_us"
tools:layout="@layout/fragment_about_us">
<action
android:id="@+id/action_nav_about_us_to_nav_feedback"
app:destination="@id/nav_feedback"
app:exitAnim="@anim/fragment_fade_exit"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
您仍在执行 ManageFragments.replaceFragment((FragmentActivity) getContext(), subCategoryFragment);
- 您无法在使用导航时执行 FragmentTransactions。
将该代码替换为调用 navigate(R.id.nav_sub_category)
以导航至您的 SubCategoryFragment
。