Reputation: 1211
Apologies as I am an Android novice. I'm trying to programmatically add multiple list fragments to a single activity, but when I do, only one is displayed. How can I display multiple lists in a single action?
The end goal is to read a set of data from an API and categorize it into multiple lists in my application. I would like the data to be horizontally scrolling list fragments, but since that's an additional complication I've started with simple ListFragments. My code looks like this:
activity_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
ItemActivity:
public class ItemActivity extends FragmentActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager manager = getSupportFragmentManager();
ItemListFragment fragment1 = new ItemListFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragmentContainer, fragment1);
ItemListFragment fragment2 = new ItemListFragment();
transaction.add(R.id.fragmentContainer, fragment2);
transaction.commit();
}
}
ItemListFragment:
public class ItemListFragment extends ListFragment {
List<String> items = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
items.add("One");
items.add("Two");
items.add("Three");
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
setListAdapter(adapter);
}
}
Upvotes: 0
Views: 2580
Reputation: 2518
Why wouldnt you do it this way?
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
// work here to change Activity fragments (add, remove, etc.). Example here of adding.
fragmentTransaction.add (R.id.myFrame, myFrag);
fragmentTransaction.commit ()
i found this code, so as just one is displayed, commit each transaction in your code.
FragmentManager manager = getSupportFragmentManager();
ItemListFragment fragment1 = new ItemListFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragmentContainer, fragment1).commit();
transaction = manager.beginTransaction(); // might be unnescessary
ItemListFragment fragment2 = new ItemListFragment();
transaction.add(R.id.fragmentContainer, fragment2).commit();
if this throws some sort of error, you might need to start another transaction for adding the second fragment.
Upvotes: 0
Reputation: 773
If you want multiple lists in one Activity
, here's what I usually do:
ListView
is using independent scrolling view, I usually split the screen for every ListView I have. Example: with 2 ListView
i split the height of the screen 50/50 so every ListView
have 50% portion of the screen.ListView
to the screen, I use cwac merge adapter to merge the adapter and display it in a single ListView
ViewPager
to display ListFragment
. This would achieve what you want, which is having multiple listviews in a single activity.Upvotes: 1