Fragments Viewpager different screen sizes

I have a FragmentActivity that uses 2 different layouts depends on the screen size:

  • xLarge Landscape. The layout has 3 LinearLayouts which hold 1 fragments each.

  • Rest screens sizes. The layout has 1 ViewPager which holds all fragments.

  • The application crashes when the screen orientation changes in 10'' screen. It uses screen size 1 in landscape mode and size 2 in portrait mode.

    The code:

    public class RegistroAccionesPartidoActivity extends FragmentActivity {
    
    
    private static ArrayAdapter<Fragment> mFragmentArray;
    private ViewPager mMyPager;
    private Fragment1 mFragment1;
    
    private Fragment2 mFragment2;
    private Fragment3 mFragment3;
    private MyFragmentAdapter mFragmentsAdapter;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mFragmentTrasaction;
    
    
    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState); 
        setContentView(R.layout.my_layout); 
    
    
        LinearLayout layoutFragment1 = (LinearLayout)findViewById(R.id.fragment1);
    
    
        if(layoutFragment1 == null) {
    
            mFragmentManager = getSupportFragmentManager();
    
            mMyPager = (ViewPager) findViewById(R.id.my_pager);
            mFragmentArray = new ArrayAdapter<Fragment>(getApplicationContext(),android.R.layout.simple_list_item_1);
            mFragmentsAdapter = new Fragment2Adapter(mFragmentManager);
    
    
            if(savedState == null) { 
    
                mFragment2 = new Fragment2(true);
                mFragment3 = new Fragment3();
                mFragment1 = new Fragment1(mIdPartido, mIdEquipoLocal, mIdEquipoVisitante);
            } else {
    
                mFragment1 = (Fragment1) getSupportFragmentManager().getFragment(savedState, "fragment1");
                mFragment2 = (Fragment2) getSupportFragmentManager().
                getFragment(savedState,"fragment2");
                mFragment3 = (Fragment3) getSupportFragmentManager().
                getFragment(savedState, "fragment3");
            }
    
    
            mFragmentArray.add(mFragment1);
            mFragmentArray.add(mFragment2);
            mFragmentArray.add(mFragment3);
    
            mDatosPartidoPager.setAdapter(mFragmentsAdapter);
    
            mDatosPartidoPager.setCurrentItem(1);
    
    
           } else { // xLarge Version
    
            mFragmentManager = getSupportFragmentManager();
            mFragmentTrasaction = mFragmentManager.beginTransaction();
    
    
            if(savedState == null) {
                mFragment1 = new Fragment1();
                mFragment2 = new Fragment2();
                mFragment3 = new Fragment3();
    
    
            } else {
                mFragment1 = (Fragment1) getSupportFragmentManager().
                    getFragment(savedState, "fragment1");
    
                mFragment2 = (Fragment2) getSupportFragmentManager().
                    getFragment(savedState,"fragment2");
                mFragment3 = (Fragment3) getSupportFragmentManager().
                    getFragment(savedState, "fragment3");
        }
        mFragmentTrasaction.add(R.id.fragment1, mFragment1);
        mFragmentTrasaction.add(R.id.fragment2, mFragment2);
        mFragmentTrasaction.add(R.id.fragment3, mFragment3);
        mFragmentTrasaction.commit();
        }
    
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        getSupportFragmentManager().putFragment(outState, "fragment2" , 
            mFragment2);
        getSupportFragmentManager().putFragment(outState, "fragment3", 
            mFragment3);
    
        getSupportFragmentManager().putFragment(outState,"fragment1", 
            mFragment1);
    
        }
    }
    

    XLarge Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    
    </LinearLayout>
    
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    
    </LinearLayout>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    
    </LinearLayout>
    
    
    </LinearLayout>
    

    The rest screen sizes layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/my_pager">
    
        </android.support.v4.view.ViewPager>
    
    </LinearLayout>
    

    I have 2 diffrent crashes according to the order of the screen change:

  • Starting on landscape, the error message is:
  • java.lang.IllegalArgumentException: No view found for id 0x...... for Fragment2

  • When I start on portrait, the error message is:
  • java.lang.IllegalStateException: Fragment already added: Fragment1

    Sorry for the large question. I just tried to be clear on explaining my problem.

    Thanks a lot.

    EDIT: In big devices (like 10'') Landscape mode uses xLarge Layout and portrait mode uses the other layout.


    for me (inside the XLarge Layout) the nested LinearLayout-Elements mixing horizontal and vertical orientation look quite strange. Please have a closer look, if you intented the nesting this way.

    Greetings, Wolfgang

    链接地址: http://www.djcxy.com/p/88720.html

    上一篇: 碎片和方向改变

    下一篇: 碎片Viewpager不同的屏幕尺寸