Reputation: 335
I would like to know what is recommended way to manipulate with ActionBar
and Fragment
.
General navigation in my app is by using NavigationDrawer
. One fragment however shows music albums and I would like to use Navigation drop down to filter albums by genres. Is it proper way to do that?
How should I populate genres within ActionBar's
drop down? Should I do it in Activity
or in Fragment
?
Upvotes: 0
Views: 269
Reputation: 44
You can create a string array in the resources file with the genres listed. This is all inside the resource tag.
<string-array name="genres">
<item>music_Genre1</item>
<item>music_Genre2</item>
.....
</string-array>
Then add this to the instance of the spinner
Spinner spinner = (Spinner) findViewById(R.id.your_View);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.genres, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Or if the array is already made in the onCreateOptionsMenu(), I believe it's there, it should have a string array with the string resources there (@string/dropdown_item), and you can add more there, and, of course, add & change the string name in your resources file. Then use the onItemSelected method to interact with the drop down.
Upvotes: 1
Reputation: 18725
The ActionBar lives in the Activity always, and needs to be controlled/populated from here.
Since you are using fragments, you should use the AB lifecycle methods in the Activity (onCreateOptionsMenu() ) to change the AB items, when you switch between fragments (again this is done in your Activity, so you are already making Fragment changes, and just need to also make AB changes).
I do think you are describing a proper use case for NavDrawer, AB, and Fragment usage - it is appropriate to put filter criteria for your content fragment in the AB.
Upvotes: 1