Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use CollapsingToolbarLayout in a Fragment from Navigation Drawer

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.

like image 828
Htoo Aung Hlaing Avatar asked Jul 17 '15 11:07

Htoo Aung Hlaing


People also ask

How do you use DrawerLayout?

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.

How do I create a new fragment and navigation drawer?

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.


1 Answers

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);
}
like image 70
Somasundaram Mahesh Avatar answered Oct 11 '22 21:10

Somasundaram Mahesh