简体   繁体   中英

In Action Bar set custom view within fragment

I have an Activity that holds some fragments.
This Activity is associated with a view pager, this view pager uses FragmentPagerAdapter, so every page of view pager is treated as a fragment.
Now, suppose I have customize the action bar view in any one of the fragment, and that custom view could be seen in other fragments also.

getActivity().getSupportActionBar().setCustomView(R.layout.custom_view_home);
        getActivity().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);   

This is because, we are customising the view, using the activity context.
My Question :
Q. Cant we set the Custom View of action bar within a fragment ? So, it would not get reflected to other fragment.

Short answer: Yes .

You should only allow currently visible fragment to add custom view to the ActionBar . Of course, you can do it right from the Fragment, with whichever context.

Requesting invalidation of the options menu will remove current custom view with the new one, ie invalidating entire ActionBar. Similar approach you can use from the link above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM