TabLayout不会填充

时间:2015-08-17 19:56:44

标签: java android android-fragments android-tablayout

我从一些Design Support Library资源开始,特别是TabLayout。

这是一个简单的片段,但是我无法填充我的TabLayout,片段正常滑动,但我的TabLayout没有得到标题的普及,正如我们在附图中看到的那样。 我不确定是否应该将ViewPager与TabLayout一起包装在片段中,这对我来说很有意义。

关注,谢谢。

http://postimg.org/image/x1z6zvlqd/

主片段Xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_sliding_tabs_about"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        android:background="@color/yellow"
        />

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

</LinearLayout>

主要片段java

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_about, container, false);
        ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.vp_about_fragment_content);
        mAdapter = new AboutPageAdapter(getFragmentManager(),
                getContext());
        viewPager.setAdapter(mAdapter);


        TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tab_sliding_tabs_about);
        tabLayout.setupWithViewPager(viewPager);
        mAdapter.notifyDataSetChanged();
        return rootView;
    }

页面适配器java

public class AboutPageAdapter extends FragmentPagerAdapter {

    final   int    PAGE_COUNT  = 2;
    private String tabTitles[] = new String[]{"Tab A", "Tab B"};
    private Context context;

    public AboutPageAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return FragA.newInstance(position);
            default:
                return FragB.newInstance(position);
        }

    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

2 个答案:

答案 0 :(得分:1)

感谢您的所有提示,我能够使用它:

python manage.py dumpdata auth.User --indent 4 > users.json

答案 1 :(得分:1)

&#34;官方&#34;解决方法: https://code.google.com/p/android/issues/detail?id=180462#c17

if (ViewCompat.isLaidOut(tabLayout)) {
        tabLayout.setupWithViewPager(viewPager);
    } else {
        tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                tabLayout.setupWithViewPager(viewPager);

                tabLayout.removeOnLayoutChangeListener(this);
            }
        });
    }