2016-11-01 275 views
0

我在使用TabLayout時遇到問題。我將設計庫更新爲v25.0.0,現在標籤標題沒有顯示出來。這裏是我的代碼:標籤標題不顯示

public void setupTabs() { 
    animateUserProfileOptions(); 
    setupViewPager(viewPager); 
} 

public void setupViewPager(final ViewPager viewPager) { 
    TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager()); 
    Fragment news = new NotificationNews(); 
    Fragment request = new NotificationRequests(); 

    adapter.addFragment(news , getApplicationContext().getString(R.string.notification_news)); 
    adapter.addFragment(request, getApplicationContext().getString(R.string.notification_request)); 
    viewPager.setAdapter(adapter); 
    tlUserProfileTabs.post(new Runnable() { 
     @Override 
     public void run() { 
      tlUserProfileTabs.setupWithViewPager(viewPager); 
     } 
    }); 
} 

這裏是我的適配器:

public class TabPagerAdapter extends FragmentPagerAdapter { 

    private final List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public TabPagerAdapter(android.support.v4.app.FragmentManager manager) { 
     super(manager); 
    } 

    @Override 
    public android.support.v4.app.Fragment getItem(int position) { 
     return mFragmentList.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mFragmentList.size(); 
    } 

    public void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 

} 

這裏是我的XML佈局 enter code here

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/home_primary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tlUserProfileTabs" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     android:background="#f2f2f2" 
     app:tabGravity="fill" 
     app:tabIndicatorColor="#28A9E0" 
     app:tabTextColor="#999999" 
     app:tabSelectedTextColor="#28A9E0" 
     app:tabTextAppearance="@drawable/tab_selector" 
     app:tabIndicatorHeight="4dp" 
     app:tabMode="fixed" /> 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

我缺少的東西還是什麼?任何幫助將不勝感激,謝謝。

+0

您不應該使用'getApplicationContext'。你應該使用'Activity'作爲'Context'。爲什麼你要創建'Runnable'來設置'TabLayout'? – Bryan

+0

是的,即時通訊添加我發佈在代碼中的runnable @Bryan –

+0

*爲什麼*你要添加一個'Runnable'?沒有理由。 – Bryan

回答

2

嘗試刪除tabTextAppearance

app:tabTextAppearance="@drawable/tab_selector" 

它可能與th相沖突e應用程序選擇的文本顏色和文本顏色

+0

救生員兄弟謝謝你很多 –

0

TabPagerAdapter應該看起來更像是這樣的:

public class TabPagerAdapter extends FragmentPagerAdapter { 

    private final ArrayMap<CharSequence, Fragment> mData = new ArrayMap<>(); 

    private TabPagerAdapter(FragmentManager manager { 
     super(manager); 
    } 

    public void addFragment(CharSequence title, Fragment fragment) { 
     mData.put(title, fragment); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return mData.valueAt(position); 
    } 

    @Override 
    public int getCount() { 
     return mData.size(); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mData.valueAt(position); 
    } 

} 

而設置應該看起來更像是這樣的:

public void setupViewPager() { 
    TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager()); 
    adapter.addFragment(getString(R.string.notification_news, new NotificationNews()); 
    adapter.addFragment(getString(R.string.notification_request, new NotificationRequest()); 

    mViewPager.setAdapter(adapter); 
    mTabLayout.setupWithViewPager(mViewPager); 
} 
+0

我試了一下,沒有任何改變你的代碼簡化了我的更多@Bryan –

+0

@MostafaAddam然後它可能是你的佈局,或可能是你的'活動'的代碼。你是否自定義了你的'TabLayout'的風格?你可以發佈「Activity」代碼和你的XML嗎? – Bryan

+0

檢查我編輯我的問題@Bryan –