2013-01-19 47 views
0

平板電腦爲每個屏幕應用程序分爲四個部分(四個片段)。我在一個佈局中添加了四個片段來碎片事務。現在我必須在那裏實現視圖尋呼機。我怎樣才能做到這一點。使用該視圖尋呼機庫,我必須傳遞片段管理器和片段列表作爲參數。在我的場景中,我怎樣才能在一次中傳遞每個4個參數?如何在具有四個碎片的屏幕中實現視圖尋呼機?

我的主要活動是:

> public void onCreate(Bundle savedInstanceState) { 
>  super.onCreate(savedInstanceState); 
>  setContentView(R.layout.activity_main);   NewsFragment[] 
> newsFragment_obj = new NewsFragment[GlobalValues.titile.length]; 
> 
>  fragMentTra = getFragmentManager().beginTransaction(); 
> 
>  for (int i = 0; i < GlobalValues.titile.length; i++) { 
>   newsFragment_obj[i] = new NewsFragment(GlobalValues.titile[i], 
>     GlobalValues.content[i]);  } 
> 
>  fragMentTra.add(R.id.fragment_container1, newsFragment_obj[0], 
>    "Fragment1");  fragMentTra.add(R.id.fragment_container2, newsFragment_obj[1], 
>    "Fragment2");  fragMentTra.add(R.id.fragment_container3, newsFragment_obj[2], 
>    "Fragment3");  fragMentTra.add(R.id.fragment_container4, newsFragment_obj[3], 
>    "Fragment4"); 
> 
>  fragMentTra.commit(); } 

這是我已經添加了四個片段到屏幕上。現在只需要查看視圖尋呼機。那麼你能否告訴我如何通過帶有示例代碼的查看尋呼機來實現這一點。

我的XML文件是:

<LinearLayout 
    android:id="@+id/upper" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/up_left_area" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#ffff66" > 

     <FrameLayout 
      android:id="@+id/fragment_container1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/up_right_area" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#ccffff" > 

     <FrameLayout 
      android:id="@+id/fragment_container2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/lower" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/down_left_area" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#66cc33" > 

     <FrameLayout 
      android:id="@+id/fragment_container3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/down_right_area" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#cc6600" > 

     <FrameLayout 
      android:id="@+id/fragment_container4" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 
</LinearLayout> 

+0

做一個包裝片段,並使用嵌套片段? (這些是在新的支持庫中引入的) – Tobrun

+0

@ user1281750你能否詳細解釋一下。再次符合我的情況。 –

回答

0

你並不需要使用FragmentTransactions可言。如果您希望在每個屏幕上都有4個「swipble」屏幕,請查看FragmentPagerAdapter,作爲Fragment

你想最主要的是從你的XML得到ViewPager

mPager = (ViewPager)findViewById(R.id.pager);

那麼你一定要通過擴展FragmentPagerAdapter和定義片段的建設,以創建自定義FragmentPagerAdapter

public static class MyAdapter extends FragmentPagerAdapter { 
    public MyAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public int getCount() { 
     return NUM_ITEMS; // In this case, 4 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // This is basically just making a new Fragment 
     // e.g. if (position == 0) return new FirstPageFragment() 
     return ArrayListFragment.newInstance(position); 
    } 
} 

然後製作適配器:

mAdapter = new MyAdapter(getSupportFragmentManager());

然後設置在ViewPager適配器:

mPager.setAdapter(mAdapter);

沒有與此陷阱的很多。例如,ViewPager處理片段標籤的設置,所以你必須非常小心休閒活動(見ViewPager and fragments — what's the right way to store fragment's state?關於此的討論)。

還有很多事情你需要考慮。如果他們不在屏幕上,你想保留碎片嗎?如果你只想要一些呢?看看FragmentStatePagerAdapterViewPager.setOffscreenLimit

+0

不,不,這不是我的場景。我想將一個屏幕分爲四個部分。用碎片填充每個部分。我想連續的屏幕也是這樣。 –

+0

您對每個屏幕使用單個片段的情況的回答。但對於我的情景表,每個屏幕有四個片段。 –

相關問題