-2

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

更新后,当我使用来自支持库。

有趣的是,在我旋转屏幕后,选项卡文本和滚动条出现了,并且仍然存在 - 我不确定这是我的代码(发布在下面)还是库中的错误。当我回到“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));
    }
}
4

1 回答 1

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);
        }
    });
}
于 2015-08-13T16:49:12.103 回答