2013-02-11 73 views
3

我的应用程序有一个列表和详细信息视图,我允许用户在不同的详细信息视图之间滑动,并使用纵向的ViewPager。在风景中,我在屏幕上显示了这两个片段。将findFragmentById与ViewPager一起使用

我有一个问题,当设备旋转时,图标在Actionbar中一遍又一遍地自我复制。我已经了解到,无论何时旋转设备,我都可能创建新的片段,我应该检查片段是否在FragmentManager中,如果不是,则创建一个新的实例。

我能够使用横向布局轻松实现,但是,我不确定如何使用ViewPager检查片段是否存在纵向模式。我在FragmentStatePagerAdaptergetItem()中这样做吗?

这是我使用的代码:

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> 

名单神父agment

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); 
    } 
} 

回答

2

好,this question's解决方案似乎为我工作。只要设置:

super.onCreate(null); 

ActivityonCreate解决了我的问题的片段。

+1

听起来像一个非常糟糕的主意,在这个过程中出现了许多其他功能。 – kaay 2016-04-20 10:53:47

相关问题