问题
Can I use CollapsingToolbarLayout in a Fragment from Navigation Drawer. I try this sample from AndroidHive. I want "Messages" to Collapsable to NestedScrollView.In a Activity is OK,but it is in a Fragment. CollapsingToolbarLayout cannot hover to Original Toolbar.But which I want to try may be the wrong pattern.Please advise me how it would be.
回答1:
I'm removing the actual activities toolbar on the Fragment's onResume
and re-enabling the activities toolbar on the Fragment's onStop
.
Please, add this code on your fragment:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
setHasOptionsMenu(true);
...
}
@Override
public void onStop() {
super.onStop();
final Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.app_bar);
getActivity().findViewById(R.id.app_bar).setVisibility(View.VISIBLE);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
and
@Override
public void onResume() {
super.onResume();
getActivity().findViewById(R.id.app_bar).setVisibility(View.GONE);
final Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
来源:https://stackoverflow.com/questions/31474401/can-i-use-collapsingtoolbarlayout-in-a-fragment-from-navigation-drawer