Can I use CollapsingToolbarLayout in a Fragment from Navigation Drawer

≡放荡痞女 提交于 2019-12-07 03:16:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!