Question

Currently I have an activity that setContentView of a layout which contains a listfragment. Basically it loads a listview.

My actionbar contains a spinner which lets me load in different lists. I could just load the List Objects in the background, and update the one fragment's adapter when the user switches to those lists using the spinner options. But this would not retain the position that the user has scrolled to.

I want to mess around with loading multiple listfragments, and when the user chooses a different spinner option, a fragment transaction happens which displays one of the other listfragments associated with that particular list.

How would I do this, and how would I modify my host activity?

Examples where I have seen FragmentTransaction used involved creating all of the fragments in the onCreate method of the host activity, and no fragments in the XML. Right now I have one fragment in the XML and need to create two more ListFragments that are practically identical.

Insight appreciated

Was it helpful?

Solution 2

It appears you can programatically scroll a ListView from here:

For a direct / smooth scroll respectively:

 getListView().setSelection(21);

 getListView().smoothScrollToPosition(21);

In the getView method of the ListView you can record the position of the last View rendered

 @Override
 public View getView(int position, View arg1, ViewGroup arg2) {
 // TODO Auto-generated method stub

Thus, when you reset your listview, you can scroll to the previous position the user was at, by keeping just an integer record of their scroll position.

I realize you're also considering a fragment solution. I know that the "replace" methods cause the replaced fragment to destroy it's view (at least that's how "support" fragments handle it). Thus in that scenario, you're still returning to a listview that's lost its scrolled state. You may try hide/show fragment transactions, as they may retain their views, and hence scroll state.

If you do try the hide/show tell me the result :) If it works, I may use it myself.

OTHER TIPS

The reason you see fragments being added in the onCreate() method as opposed to putting them directly in the XML is because fragments that are contained within an XML cannot be removed/modified. If you must have this fragment and want to load in other fragments, you can add containers (like LinearLayout, RelativeLayout, etc.) to the parent container and then set their properties dynamically through code. An example would be:

LinearLayout layoutD = new LinearLayout(this);
layoutD.setId(R.id.layoutd);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
layoutD.setLayoutParams(params);
layoutD.setOrientation(LinearLayout.VERTICAL);
parent.addView(layoutD); // parent is a reference to the parent widget in the XML
getSupportFragmentManager.beginTransaction().add(R.id.layoutd, your_fragment_here).addToBackStack(null).commit();

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top