2012-07-13 51 views
1

滑動式標籤,我想知道是否有可能適應我的實際標籤不使用片段創建滑動式標籤。不使用片段

現在我有非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但我失敗了。

+2

爲什麼你不希望使用片段?轉換到它們相當容易。 – 2012-07-13 19:26:06

+0

最簡單的方法是什麼?我想知道我的無盡列表視圖是否仍然可以使用,我在我的應用程序的任何地方都會使用它(在一些沒有製表符的地方) – Fr4nz 2012-07-13 19:32:15

+0

碎片和活動具有相似的生命週期,因此您可以將相關內容提取到碎片的正確部分。唯一可能非常棘手的部分就是將片段附加到您的活動中,但有很多示例都是這樣做的。只要記住Fragment沒有Context,所以你必須經常使用getActivity()。 – 2012-07-13 20:14:21

回答

2

您可以通過使用

第二個選項是更好,因爲你可以使用幾乎所有的東西作爲擴展「頁面'內的發射器(就像安卓主屏)

這就是說,它會容易得多使用片段,然後使用就可以了ViewPager :)

+0

我能使用ViewPager +視圖代替片段 – Fr4nz 2012-07-16 08:56:30

+0

我想你也許可以做到這一點,檢查出的TextView是如何加入到ViewPager底部?此鏈接http://android-developers.blogspot.in/2011/08/horizo​​ntal-view-swiping-with-viewpager.html的 – Soham 2012-07-17 18:48:49

+0

其實我發現我的幸福在這裏 - > HTTP:// viewpagerindicator.com/ – Fr4nz 2012-07-18 06:50:35