Android Tablayout与viewpager配对后没有标签标题

时间:2016-10-01 19:34:52

标签: android android-viewpager android-tablayout

我只想尝试将TabLayout与ViewPager配对。配对几乎正常,但Tabtexts后来消失了。

布局:     

            <android.support.design.widget.TabItem
                android:id="@+id/group_tab_orders"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:text="@string/orders"/>

            <android.support.design.widget.TabItem
                android:id="@+id/group_tab_members"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/members"/>

            <android.support.design.widget.TabItem
                android:id="@+id/group_tab_meals"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/meals"/>

        </android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/group_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

代码:

tabLayout = (TabLayout) rootView.findViewById(R.id.group_tablayout);
    viewPager = (ViewPager) rootView.findViewById(R.id.group_viewpager);

    reference = FirebaseDatabase.getInstance().getReference("groups").child(getArguments().getString("key"));

    viewPager.setAdapter(new GroupContentAdapter(getFragmentManager(), reference));
    tabLayout.setupWithViewPager(viewPager);

我也意识到,通过viewPager.setAdapter(new GroupContentAdapter(getFragmentManager(), reference));删除标题不会被修改,所以这里是适配器的代码

GroupContentAdapter.java

public class GroupContentAdapter extends FragmentPagerAdapter {

public static int SIZE = 3;

private GroupContentFragment[] contents;
private DatabaseReference reference;


public class GroupContentAdapter extends FragmentPagerAdapter {

public static int SIZE = 3;

private GroupContentFragment[] contents;
private DatabaseReference reference;


public GroupContentAdapter(FragmentManager manager, DatabaseReference reference) {
    super(manager);
    this.reference = reference;
    contents = new GroupContentFragment[SIZE];
    contents[0] = GroupContentFragment.newInstance(reference.child(Utils.GROUPORDERS), ContentType.ORDERS);
    contents[1] = GroupContentFragment.newInstance(reference.child(Utils.GROUPMEMBERS), ContentType.MEMBERS);
    contents[2] = GroupContentFragment.newInstance(reference.child(Utils.GROUPMEALS), ContentType.MEALS);
}

@Override
public int getCount() {
    return SIZE;
}

@Override
public Fragment getItem(int position) {
    return contents[position];
}

}

2 个答案:

答案 0 :(得分:0)

您需要覆盖FragmentPageAdapter的.perc-neg.good:before(#98F1AC) )方法。见here

答案 1 :(得分:0)

在适配器类中添加以下内容:

@Override
public CharSequence getPageTitle(int position) {
    if (position == 0)
        return "Text1";
    else
        return "Text2";
}

MainActivity内,我在onCreate内添加了以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Add a View Pager with its settings
    viewPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fragmentManager = getSupportFragmentManager();
    viewPager.setAdapter(new MyAdapter(fragmentManager));

    pageIndicatorView = (PageIndicatorView) findViewById(R.id.pageIndicatorView);
    pageIndicatorView.setViewPager(viewPager);

    // Add toolbar with its settings
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Custom Title");
    setSupportActionBar(toolbar);

    // Add tabLayout and link it to the ViewPager.
    tabLayout = (TabLayout) findViewById(R.id.tablayout);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.getTabAt(0).setIcon(R.drawable.image1);
    tabLayout.getTabAt(1).setIcon(R.drawable.image2);
}

这就是你在文字消失之后添加图标的方式。

希望这个答案有所帮助。