滑動式標籤,我想知道是否有可能適應我的實際標籤不使用片段創建滑動式標籤。不使用片段
現在我有非swipble選項卡(每個選項卡包含一個無盡的列表:NewsList)。
下面是我用我的非滑動式標籤的代碼:
tabHost = (TabHost)this.findViewById(R.id.scrollTabs);
// Avant d’ajouter des onglets, il faut impérativement appeler la méthode
// setup() du TabHost
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
// Ajoute un onglet pour chaque catégorie de news
tabNewslist = new NewsList[NewsCategory.values().length];
View tabView;
for (int i = 0; i < NewsCategory.values().length; i++) {
tabView = createTabView(tabHost.getContext(), NewsCategory.getValueAt(i).getName());
tabNewslist[i] = new NewsList(this, new LinkedList<Item>(), IdUrlRss.NEWSLIST, NewsCategory.getValueAt(i));
tabHost.addTab(tabHost.newTabSpec(NewsCategory.getValueAt(i).getName()).setIndicator(tabView).setContent(tabNewslist[i]));
}
而這裏的XML佈局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
我也試圖創建新聞列表一個PagerAdapter但我失敗了。
爲什麼你不希望使用片段?轉換到它們相當容易。 – 2012-07-13 19:26:06
最簡單的方法是什麼?我想知道我的無盡列表視圖是否仍然可以使用,我在我的應用程序的任何地方都會使用它(在一些沒有製表符的地方) – Fr4nz 2012-07-13 19:32:15
碎片和活動具有相似的生命週期,因此您可以將相關內容提取到碎片的正確部分。唯一可能非常棘手的部分就是將片段附加到您的活動中,但有很多示例都是這樣做的。只要記住Fragment沒有Context,所以你必須經常使用getActivity()。 – 2012-07-13 20:14:21