Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot add multiple fragments to LinearLayout

I am using a LinearLayout with a vertical orientation to list fragments. I add fragments to the container programmatically like this:

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment fragment1 = new Fragment();
ft.add(R.id.llContainer, fragment1);

Fragment fragment2 = new Fragment();
ft.add(R.id.llContainer, fragment2);

ft.commit();

But it only shows the first fragment. Why?

like image 974
ferpar1988 Avatar asked Mar 21 '14 11:03

ferpar1988


1 Answers

Had the same problem, but using

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.control_list, container, false);

I.E. using a xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/control_list">
</LinearLayout>

Solved my problem. When creating the LinearList programmatically only the first fragment showed up for me too.

That is using an xml file for the layout you want to add the fragments to.

like image 181
Erik Avatar answered Oct 02 '22 14:10

Erik