碎片Viewpager不同的屏幕尺寸

我有一个使用2种不同布局的FragmentActivity取决于屏幕大小:

  • xLarge风景。 该布局有3个LinearLayouts ,每个容纳1个片段。

  • 休息屏幕大小。 该布局具有1个ViewPager ,其中包含所有片段。

  • 屏幕方向在10英寸屏幕中更改时,应用程序崩溃。 它在横向模式下使用屏幕尺寸1,在纵向模式下使用尺寸2。

    代码:

    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布局:

    <?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>
    

    其余的屏幕尺寸布局:

    <?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>
    

    根据屏幕更改的顺序,我有2个不同的崩溃:

  • 从横向开始,错误消息是:
  • java.lang.IllegalArgumentException:对于Fragment2的id 0x ......没有找到视图

  • 当我开始纵向时,错误消息是:
  • java.lang.IllegalStateException:片段已经添加:Fragment1

    对大问题抱歉。 我只是试图解释我的问题。

    非常感谢。

    编辑:在大设备(​​如10“)横向模式使用xLarge布局和肖像模式使用其他布局。


    对于我来说(在XLarge Layout中),嵌套的LinearLayout-Elements混合水平和垂直方向看起来很奇怪。 如果你以这种方式嵌套,请仔细观察。

    问候,沃尔夫冈

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

    上一篇: Fragments Viewpager different screen sizes

    下一篇: Orientation change using fragments