3

我的应用程序有一个列表和详细视图,我允许用户在不同的详细视图之间滑动,使用ViewPager纵向。在横向中,我在屏幕上显示两个片段。

我遇到了一个问题,当设备旋转时,图标在Actionbar. 我了解到,每当设备旋转时,我可能会创建新的片段,我应该检查片段是否在其中FragmentManager,如果不是,则创建一个新实例。

我可以使用横向布局轻松做到这一点,但是,我不确定如何使用ViewPager. 我会这样做getItem()FragmentStatePagerAdapter

这是我正在使用的代码:

public class Main extends SherlockFragmentActivity
{
    private static List<Integer> mIds;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager); //view pager exists, so we are using the portait layout

        if (mViewPager != null)
        {
            mIds = new ArrayList<Integer>();

            mIds.add(0);
            mIds.add(1);
            mIds.add(2);
        }
        else //in landscape
        {           
            ListFragment lf = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);

            if (lf == null)
                lf = new ListFragment();

            DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentDetail);

            if (df == null)
            {
                df = new DetailFragment();
                df.setArguments(getIntent().getExtras());   
            }

            getSupportFragmentManager().beginTransaction().add(R.id.fragmentList, lf).commit();
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentDetail, df).commit();
        }
    }       

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {        
            //can't use getSupportFragmentManager().findFragmentById() here because I get a "Cannot make a static reference to the non-static method" error
            if (index == 0)
                return ListFragment.newInstance();
            else            
                return DetailFragment.newInstance(mIds.get(index-1));
        }  

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

主布局(纵向)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
     />
 </RelativeLayout>

主布局(横向)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:weightSum="3"
>
    <FrameLayout 
        android:id="@+id/fragmentList"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
     />

    <FrameLayout 
        android:id="@+id/fragmentDetail"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="fill_parent"
     />

</LinearLayout>

列出片段

public class ListFragment extends SherlockListFragment implements DialogKeywordAddListener
{
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);
    }

    public static ListFragment newInstance() {

        return new ListFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        return inflater.inflate(R.layout.listing, container, false);
    }
}

细节片段

public class DetailFragment extends SherlockListFragment
{
    private int mId;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);
    }

    public static DetailFragment newInstance() {

        DetailFragment df = new DetailFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        df.setArguments(bundle);

        return df;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        if (getArguments() != null)
            mId = getArguments().getInt("id");

        return inflater.inflate(R.layout.detail, container, false);
    }
}
4

1 回答 1

2

好吧,这个问题的解决方案似乎对我有用。只是设置:

    super.onCreate(null);

解决了我onCreateActivity片段问题。

于 2013-02-16T20:24:17.527 回答