简体   繁体   中英

How can I programmatically add multiple list fragments to a single activity?

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);
    }
}

If you want multiple lists in one Activity , here's what I usually do:

  1. Because every 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.
  2. If I have to dynamically add ListView to the screen, I use cwac merge adapter to merge the adapter and display it in a single ListView
  3. Another alternative you can just use ViewPager to display ListFragment . This would achieve what you want, which is having multiple listviews in a single activity.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM