Android设计支持库和TabLayout

时间:2015-08-07 12:32:28

标签: android android-viewpager android-tablayout

我有一个有趣的问题(如果这是正确的话!)我最近更新到最新的Android设计支持库' com.android.support:design:22.2 0.1'从' com.android.support更新:design:22.2.0'

更新后,我注意到当我运行应用程序(在不同的手机和不同版本的Android上试用 - 4.4.4和5.1.1)时,Tab文本和标签滚动条没有显示出来的奇怪事情来自支持库的新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>

片段Java类

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

1 个答案:

答案 0 :(得分:2)

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