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.
To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.
To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With