I have an app where the MainActivity which creates an ActionBar with tabs (used ActionBarSherlock for this).
Each tab loads a Fragment. One fragment has a ListView in which I load items. Each item responds to an OnItemClick event.
When an item is clicked then the app gets data from a webserver. I want to show that data in a new ListView. But that’s where i’m stuck.
How can I switch from my first main ListView to a ‘sub-ListView`?
Keep in mind that my current View is a fragment and that it’s loaded inside a tab. I want to remain on the same tab when I show the new ‘sub’ ListView.
Also, when I push the back button on the Android device, then I want it to show the main ListView again.
So actually, kinda like the iPhone does it with their TableView.
You have te use another
Fragmentin order to do that. You need to create thatFragmentand then replace the old one with theFragmentTransactionand if you want to use the “back” button you need to add the new fragment to theBackstack.If these
ListFragmentare quite different in terms of data, I’d have 2 fragment classes (e.g. : FirstListFragment, SecondListFragment).Here’s some code from an App I worked on :
The R.id.fraglist should be defined in your XML layout (except if you programmatically created your layout). It’s simply the ID of your default fragment.
You can also use a Bundle to parse data like so :