使用TabLayout
问题标题是不可见的我在适配器中覆盖getPageTitle
我也返回了一个字符串,该字符串将是标题但该字符串不会显示。我调试了应用程序,我从服务器获取标题。
是的,我跟着this,但它也没有用。
这是相关的java代码和xml文件。
private void loadCategories() {
StringRequest request = new StringRequest(Constants.URL_GET_CATEGORIES, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
categoriesDTO = Constants.gson.fromJson(response,CategoriesDTO.class);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
setTabLayout();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
ApplicationController.getmInstance().addToRequestQueue(request);
}
private void setTabLayout(){
tabLayout.setupWithViewPager(viewPager);
}
viewpager的适配器。
private class ViewPagerAdapter extends FragmentStatePagerAdapter{
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new ContentFragment();
}
@Override
public int getCount() {
return categoriesDTO.getCategories().size();
}
@Override
public CharSequence getPageTitle(int position) {
String title =categoriesDTO.getCategories().get(position).getTitle();
return title;
}
}
这是xml。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</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="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/home_drawer_menu">
</android.support.design.widget.NavigationView>
这是输出
这是gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.softoven.ultron"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'org.jsoup:jsoup:1.6.1'
compile 'com.mikhaellopez:circularimageview:3.0.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.code.gson:gson:2.5'
}
如何让它发挥作用?
答案 0 :(得分:3)
在我的情况下,我通过替换来解决这个问题,
tabLayout.setupWithViewPager(viewPager);
通过,
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
我希望这会帮助你。
答案 1 :(得分:2)
我在tablayout xml中进行了以下更改,然后标题可见
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#fff"
app:tabGravity="fill"
app:tabMode="scrollable"
>
答案 2 :(得分:1)
这将为你做Trick。在Runnable中添加
Tab Layout
。
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
答案 3 :(得分:0)
在你的风格中:
List<CodeItemsRule> _CodeItemRules = new List<CodeItemsRule>()
{
new CodeItemsRule()
{
CodeID="00009",
ItemID="D1",
RuleID=2
},new CodeItemsRule()
{
CodeID="00009",
ItemID="D2",
RuleID=2
},new CodeItemsRule()
{
CodeID="00009",
ItemID="D3",
RuleID=1
},new CodeItemsRule()
{
CodeID="00008",
ItemID="D1",
RuleID=3
},new CodeItemsRule()
{
CodeID="00007",
ItemID="D3",
RuleID=1
},new CodeItemsRule()
{
CodeID="00007",
ItemID="D4",
RuleID=1
},new CodeItemsRule()
{
CodeID="00010",
ItemID="D3",
RuleID=2
},new CodeItemsRule()
{
CodeID="00010",
ItemID="D1",
RuleID=1
},new CodeItemsRule()
{
CodeID="00010",
ItemID="D2",
RuleID=1
}
};
var pivotTable = _CodeItemRules.ToPivotTable(
item => item.ItemID,
item => item.CodeID,
items => items.Any() ? items.Sum(x => x.RuleID) : 0);
在您的XML中:
<style name="NavigationTabTextAppeareance" parent="TextAppearance.Design.Tab">
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
</style>