配置更改后,在ViewPager中替换片段

时间:2013-12-27 23:01:17

标签: android android-fragments android-viewpager android-orientation

我有一个有2页的ViewPager。第一页是具有不同布局的列表,具体取决于设备的类型和方向。在纵向模式的情况下,列表是经典列表,在横向模式下,应显示网格。

对于这两种布局,我创建了两个不同的片段:ResultListFragment和ResultGridFragment。

这是我的寻呼机适配器:

public static class GSPagerAdapter extends FragmentStatePagerAdapter  {

    private Context mContext;
    private FragmentManager mFragmentManager;
    private AbstractResultFragment mResultFragment;

    public GSPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        mContext = context;
        mFragmentManager = fm;
    }

    @Override
    public Fragment getItem(int i) {
        Log.v("pager", "GSPagerAdapter.getItem(" + i + ")");

        switch (i) {
        case 0: {
                // if the device is in portrait mode, use a list 

                if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ) {
                    Log.v("pager","return new list fragment");
                    mResultFragment = new ResultListFragment(); 
                    return  mResultFragment;
                // else use a grid
                } else {
                    Log.v("pager","return new grid fragment");
                    mResultFragment = new ResultGridFragment();
                    return mResultFragment;
                }   

        }
        case 1: {
            Log.v("pager", "return the new filter fragment");
            return new FilterFragment();
        }
        default: {
            Log.v("pager", "return null");
            return null;
        }
        }
    }

    @Override
    public CharSequence getPageTitle(int i) {

        switch (i) {
        case 0:
            return "Result";
        case 1:
            return "Filter";
        }
        return "";
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
     public Object instantiateItem(ViewGroup container, int position) {
        Log.v("pager","instantiateItem: "+position);


         return super.instantiateItem(container, position);
     }

    @Override
    public int getItemPosition(Object object){
        Log.v("pager","getItemPosition: "+object);
        return POSITION_NONE;
    }
}

活动的OnResume:

public void onResume() {
    super.onResume();
    Log.v("lifecycle", "MainActivity.onResume()");
    if(mViewPager != null)
        mGSPagerAdapter.notifyDataSetChanged();

} 

我做错了什么?我已经阅读了几个问题和答案,但我无法弄清楚问题是什么。

0 个答案:

没有答案