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, i.e invalidating entire ActionBar. Similar approach you can use from the link above.
Related
I Have a ViewPager in my activity which contains 5 fragments,I
fixed a menu on toolbar which used as filter, on click on menu
item I need to change views on all fragments,
I used to clear all the fragments in ViewPager and attached new instances of fragments,this displays correct views but while creating new
views I used to show a progress bar on screen but It's not showing as
we set, it calls after the change of all views.
The thing I doubt was is this correct method to change all views by replacing fragments with new instances Or there is any other method to change data in fragments respective to my context
Thank you
Edit:
clearing adapter for create new views
viewPagerAdapter.fragmentArrayList.clear();
viewPagerAdapter.fragmentTitle.clear();
viewPagerAdapter.notifyDataSetChanged();
If you want to refresh all your fragment at once you can setAdapter of ViewPager again than it will refresh your all fragments like this;
mViewPager.setAdapter(yourAdapter);
//If you want set current postion for adapter
mViewPager.setCurrentItem(theCurrentPage);
You are rightfully doubting whether destroying and recreating all your fragments is a good method if you only want to filter the list of fragments that your ViewPager is showing. It amounts to unnecessarily wasting considerable resources.
Instead you either manipulate your fragment list (and fragment title list?) and then call notifyDataSetChanged() or implement a Filter as explained here.
So you go:
viewPagerAdapter.fragmentArrayList.remove(index);
or
viewPagerAdapter.fragmentArrayList.remove(fragment);
and then you say:
viewPagerAdapter.notifyDataSetChanged();
Concerning the progress bar, where in your XML layout is it located? Within the view pager? Then move it outside. Somewhere else? Then show us your XML and the code creating and showing it.
I have a activity that implements a CustomView to inflate the ActionBar with a Checkbox.
This Checkbox is a "LikeButton" to a article from a source and when the user swipe from left/right it load another articles from the same source.
The code is working fine for the first article that the user open, but when he swipe the Checkbox stays with the same state from the firt article.
How can I build a CustomView diferent for every article and inflate it when the user swipe?
Use a ViewPager with Fragments containing your custom views. Also, for each fragment, make sure that you enable it's own options menu by calling Fragment.setHasOptionsMenu(boolean hasMenu);. Once your do this, just Override your optionsMenu methods within your fragment classes.
I need to create an activity with an ActionBar, so that if you press the action bar buttons, the contents of the activity display the same information in different layout (one is a pie chart and the other a listview).
I don't want to use tabs or a viewpager, so what would be the best way to do this?
Build each view in a separate fragment, and use the actionbar buttons to set the current visible fragment.
Is there a way to use ActionBar tabs, but with the tabs switching ContentViews instead of fragments? The issue is that I have dual-pane layouts on large screens that have a fragment in each of two panes and I'd want to switch out the whole container view when that tab is selected.
Currently I'm using my own tabbing mechanism, but I'd like to integrate better into the built-in features and behavior of the ActionBar, and to save space by not having to have a separate ActionBar and tab bar when possible.
From the Action Bar docs --
To get started, your layout must include a ViewGroup in which you
place each Fragment associated with a tab. Be sure the ViewGroup has a
resource ID so you can reference it from your tab-swapping code.
Alternatively, if the tab content will fill the activity layout
(excluding the action bar), then your activity doesn't need a layout
at all (you don't even need to call setContentView()). Instead, you
can place each fragment in the default root ViewGroup, which you can
refer to with the android.R.id.content ID (you can see this ID used in
the sample code below, during fragment transactions).
If you specify a layout for your activity with slots for two fragments, you can replace them both (or even just one if that makes sense your app) in the ActionBar.TabListener callback. The callback still leaves you in control of the layout.
ideally i'd like my main activity to have 2 views rather than starting a new activity; currently i'm using a view switcher to switch from map view and list view.
on my map view i want a transparent action bar.
on the list view i want a solid one (so the list items aren't inaccessible under the action bar).
is this something that is possible?
or am i forced to create 2 seperate activities and pass a list of custom objects through a bundle to make the transition between views as simple as possible.
the final goal is to have a iPhone style flipper animation when switching the views so it's like you're viewing the current stats/details on the back of the map view.
apparently such a transition animation might not be possible between activities, but i'm still looking into it.....
You can use actionBar.show() and actionBar.hide() to toggle ActionBar feature.