11

我有一个主要活动,它承载一个片段,该片段又承载一个 TabLayout(带有 ViewPager)。显示了选项卡栏,但未显示选项卡本身。

这是我在显示主机片段的主要活动中的代码:

        Fragment fragment = new BMITabsFragment();

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(Constants.BMI_TABS_FRAGMENT).commit();

这是我托管 TabLayout 的 Fragment,即 BMITabsFragment (sa):

public class BMITabsFragment extends Fragment {
...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
            getActivity()));

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(viewPager);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_bmitabs, container, false);
    return view;
}
...
}

这是我的 FragmentPagerAdapter:

public class BMIFragmentPagerAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 2;
private FragmentManager fragmentManager;
private Context context;

public BMIFragmentPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
    this.fragmentManager = fm;

}

public BMIFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
    fragmentManager = fm;

}

@Override
public CharSequence getPageTitle(int position) {
    String[] pageTitles = context.getResources().getStringArray(R.array.page_titles_array);
    return pageTitles[position];
}

@Override
public Fragment getItem(int position) {
    SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFS_FILE, 0);
    long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1);
    Fragment fragment = null;
    switch (position){
        case 0:
            return BMITabelleFragment.newInstance(patientId);

        case 1:
            return BMIChartFragment.newInstance(patientId);

        default:
            return BMITabelleFragment.newInstance(patientId);
    }
}

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

这是fragment_bmitabs.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.TabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@android:color/white" />

</LinearLayout>

我的代码基于https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout上的 Google Android 指南

我在这里缺少什么?

注意:我正在使用 AppCompatActivity 和支持库 v4 & v7 和 com:android:support:design 库

4

7 回答 7

51

这为我修复了它:

tabLayout.post(new Runnable() {
    @Override
    public void run() {
        tabLayout.setupWithViewPager(viewPager);
    }
});

https://code.google.com/p/android/issues/detail?id=180462

于 2015-07-22T22:00:09.747 回答
9

设置标签图标后setupWithViewPager()

private void setTabs {
   tabLayout.setupWithViewPager(viewPager);
   setupTabIcons();
} 

private void setupTabIcons() {
   tabLayout.getTabAt(0).setIcon(tabIcons[0]);
   tabLayout.getTabAt(1).setIcon(tabIcons[1]);
   tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
于 2015-10-14T13:34:23.990 回答
6

就像@Nathaniel Ford 说的,这应该是个bug,我改用设计库23.0.1。google 修复了,所以build.gradle改成compile 'com.android.support:design:23.0.1'. ps:您还必须将您的更改compileSdkVersion为23

于 2015-09-16T12:05:47.243 回答
1

其他答案都不适合我,我都试过了

但是,这个做到了:TabLayout not show tabs after added navigation bar

根据该答案,您必须将您TabLayoutAppBarLayout. 为什么?谁知道。但至少它有效。

示例代码(取自该帖子):

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp"
    android:id="@+id/appBarLayout2">


    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout2"
        app:tabMode="fixed"
        app:tabGravity="fill"

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

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

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:id="@+id/viewPager2"
    android:layout_below="@+id/appBarLayout2">

</android.support.v4.view.ViewPager>
于 2018-07-02T01:08:27.020 回答
1

如果您android:tabPadding在 xml 文件的 Tablayout 中使用属性,请将其删除。

于 2017-05-16T12:42:50.497 回答
0

您可以使用FragmentStatePagerAdapter而不是FragmentPagerAdapter。它可能会帮助你。

于 2019-09-10T10:11:13.307 回答
0

如果将来有人使用 TabLayout 和 ViewPager2 并面临同样的问题。只需.attach();在末尾添加new TabLayoutMediator().

new TabLayoutMediator(tabLayoutRef, viewPager2Ref, (tab, position) -> tab.setText("Tab No. "+position)).attach();
于 2021-08-06T11:52:09.790 回答