1

我在我的視圖中使用谷歌的SlidingTabLayout,但我想動態地添加新的標籤。 我使用這個http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html如何將標籤添加到SlidingTabLayout?

這是我的代碼初始化:

// Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
    //this^is a FragmentStatePagerAdapter 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    // Assiging the Sliding Tab Layout View 
    tabs = (SlidingTabLayout) findViewById(R.id.tabs); 

    // Setting the ViewPager For the SlidingTabsLayout 
    tabs.setViewPager(mViewPager); 

我完全不知道如何添加一個標籤。

我想過改變ViewPager但在SlidingTabLayout有一個評論:

/** 
* Sets the associated view pager. Note that the assumption here is that the pager content 
* (number of tabs and tab titles) does not change after this call has been made. 
*/ 
public void setViewPager(ViewPager viewPager) { 
+0

這可能與您設想的設計稍有不同,但請看看這個班級 - 它可以做你需要的東西,儘管沒有標籤:https://github.com/daimajia/AndroidSwipeLayout/ – 2015-02-10 19:22:30

+0

我需要標籤。但是,無論如何感謝 – maysi 2015-02-10 19:32:52

回答

0

這裏是一個很好的教程。 http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

總之,你可以爲他們提供你的尋呼機適配器。重寫getPageTitle方法以提供自定義的東西。例如。

@Override 
public CharSequence getPageTitle(int position) { 
    return _items[position].getDisplayName(); 
} 
+0

是的,它很好,但它不顯示如何動態添加新標籤 – maysi 2015-02-11 05:55:20

+0

您是否找到解決方案@Simon。我正在尋找相同的問題 – Sammys 2015-07-02 00:18:08

+0

@Sammys你試過這個答案嗎? – maysi 2015-07-02 00:37:24

3

有可能是一個更好的,更有效的方式,而只是每次調用setViewPager()你的網頁添加到您的ViewPager將工作。