Android 设计支持库和 TabLayout

Android Design Support Library and TabLayout

我遇到了一些有趣的问题(如果这是正确的词!)我最近更新到最新的 Android 设计支持库 'com.android.support:design:22.2.1' 更新自 'com.android.support:design:22.2.0'

更新后,我注意到一个 st运行ge 事情发生了,当我 运行 应用程序时,选项卡文本和选项卡滚动条不显示(在不同的手机和不同版本的 Android - 4.4.4 & 5.1.1) 使用支持库中的新 TabLayout 时。

有趣的是,在我旋转屏幕后,选项卡文本和滚动条出现并保持不变 - 我不确定这是我的代码问题(在下面发布)还是库中的错误。当我回到 'com.android.support:design:22.2.0' 时,这个问题就消失了,但我也不排除。只是想看看其他人在使用新的设计支持库时是否遇到过这个问题。

我的代码:

使用 TabLayout 的片段布局:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.treefrogapps.TaDo.TaDOChooserFragment">

    <android.support.design.widget.TabLayout
        android:id="@+id/chooserFragmentSlidingTabsLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:tabMode="fixed"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/chooserFragmentViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

片段JavaClass

public class TaDOChooserFragment extends Fragment {

    private View rootView;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    public TaDOChooserFragment() {
    }

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

    }

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

        rootView = inflater.inflate(R.layout.fragment_tado_chooser, container, false);
        return rootView;
    }

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

        initialiseTabs();
    }

    private void initialiseTabs() {

        mViewPager = (ViewPager) rootView.findViewById(R.id.chooserFragmentViewPager);
        mViewPager.setAdapter(new TaDOChooserPagerAdapter(getActivity(), getChildFragmentManager()));
        // new design library tab layout
        mTabLayout = (TabLayout) rootView.findViewById(R.id.chooserFragmentSlidingTabsLayout);
        mTabLayout.setBackgroundColor(getResources().getColor(R.color.primaryColor));
        mTabLayout.setTabTextColors(getResources().getColor(R.color.grey_light), getResources().getColor(R.color.white));
        mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

                Fragment fragment = ((TaDOChooserPagerAdapter) mViewPager.getAdapter()).getFragment(position);
                if (fragment !=null){
                    fragment.onResume();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

片段寻呼机适配器:

public class TaDOChooserPagerAdapter extends FragmentPagerAdapter {

    private String[] tabTitlesArray;
    private Context context;
    private FragmentManager fragmentManager;
    private HashMap<Integer, String> fragmentTags;


    public TaDOChooserPagerAdapter(Context context, FragmentManager fm) {
        super(fm);

        tabTitlesArray = context.getResources().getStringArray(R.array.chooser_fragment_tab_title_array);

        this.context = context;
        this.fragmentManager = fm;
        this.fragmentTags = new HashMap<>();
    }

    @Override
    public Fragment getItem(int position) {

        switch (position){
            case 0: return new TaDOChooserTabFragment1();
            case 1: return new TaDOChooserTabFragment2();
            default: return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitlesArray[position];
    }

    @Override
    public int getCount() {
        return tabTitlesArray.length;
    }

    // used for refreshing tabs after they have been loaded - uses onResume method
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Object object = super.instantiateItem(container, position);

        if (object instanceof Fragment){
            //record the fragment tag
            Fragment fragment = (Fragment) object;
            fragmentTags.put(position, fragment.getTag());
        }

        return object;
    }

    public Fragment getFragment(int position){

        if(fragmentTags.get(position) == null) return null;
        return fragmentManager.findFragmentByTag(fragmentTags.get(position));
    }
}

这是 Android 设计支持库 22.2.1 中的已知错误 https://code.google.com/p/android/issues/detail?id=180462

有一个变通办法;

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);
        }
    });
}