TabLayout 中 item 的自定义 & 使用 Fragment 的注意事项

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/zeqiao/article/details/83304096

采用默认模式:

自定义 item 后(tabitem):

在 Fragment 采用 TabLayout + ViewPager 布局:

public class OrderFragment extends Fragment {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    public OrderFragment() {

    }

    public static OrderFragment newInstance() {
        OrderFragment fragment = new OrderFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        initView(view);
        initTab();
        return view;
    }

    private void initView(View view) {
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
    }

    private void initTab() {
        String[] titles = {"待签到", "进行中", "已完成", "已取消"};
        ArrayList<Fragment> mFragmentList = new ArrayList<>();
        mFragmentList.add(FirstFragment.newInstance());
        mFragmentList.add(SecondFragment.newInstance());
        mFragmentList.add(ThirdFragment.newInstance());
        mFragmentList.add(FourthFragment.newInstance());
        OrderPagerAdapter orderPagerAdapter = new OrderPagerAdapter(getChildFragmentManager(), titles, mFragmentList);
        
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mViewPager.setAdapter(orderPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        for (int i = 0; i < titles.length; i++) {
            // 获得每一个tab
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            if (tab != null) {
                // 给每一个tab设置view
                tab.setCustomView(R.layout.item_tab_order_page);
                TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_title);
                textView.setText(titles[i]);
                if (i == 0) {
                    // 设置第一个 tab 被选中
                    tab.getCustomView().findViewById(R.id.rl_tab).setSelected(true);
                    textView.setTextColor(Color.BLACk);
                    tab.getCustomView().findViewById(R.id.tab_indicator).setBackgroundColor(Color.BLUE);
                }
            }
        }
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // mViewPager.setCurrentItem(tab.getPosition());
                tab.getCustomView().findViewById(R.id.rl_tab).setSelected(true);
                TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_title);
                textView.setTextColor(Color.BLACk);
                tab.getCustomView().findViewById(R.id.tab_indicator).setBackgroundColor(Color.BLUE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.rl_tab).setSelected(false);
                TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_title);
                textView.setTextColor(Color.GRAY);
                tab.getCustomView().findViewById(R.id.tab_indicator).setBackgroundColor(Color.WHITE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        // tab 之间的分割线
        LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.divider_vertical));
    }

}

FragmentPagerAdapter:

public class OrderPagerAdapter extends FragmentPagerAdapter {

    private String[] mTitles;
    private ArrayList<Fragment> mFragmentList;

    public OrderPagerAdapter(FragmentManager fm, String[] titles, ArrayList<Fragment> fragmentList) {
        super(fm);
        mTitles = titles;
        mFragmentList = fragmentList;
    }

	// 已经采用自定义 item,这里可以忽略掉
    /*@Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }*/

    @Override
    public Fragment getItem(int position) {
        /*Fragment fragment = mFragmentList.get(position);
        Bundle bundle = new Bundle();
        bundle.putString("title", mTitles[position]);
        fragment.setArguments(bundle);*/
        return mFragmentList.get(position);
    }

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

TabLayout + ViewPager 布局文件:

<?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_layout"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/white"
        app:tabIndicatorHeight="0dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

自定义 item 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_tab"
    android:layout_width="match_parent"
    android:layout_height="@dimen/tab_item_height">

    <TextView
        android:id="@+id/tv_tab_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center" />

    <TextView
        android:id="@+id/tv_tab_count"
        android:layout_width="@dimen/size_15"
        android:layout_height="@dimen/size_15"
        android:layout_alignParentRight="true"
        android:layout_marginTop="@dimen/size_5"
        android:background="@drawable/bg_circle_ff0000"
        android:gravity="center"
        android:text="3"
        android:textColor="@color/white"
        android:textSize="@dimen/size_10"
        android:textStyle="bold" />

    <View
        android:id="@+id/tab_indicator"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size_2"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="@dimen/size_5"
        android:layout_marginRight="@dimen/size_5"
        android:background="@color/white" />

</RelativeLayout>

右上角数量提示背景图(用 xml 当背景图效率高于用图片,且占用少):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">

    <solid android:color="#ff0000" />
    <size
        android:width="50dp"
        android:height="50dp" />

</shape>

使用 Fragment 的注意事项:常用代码整理:使用 Fragment 的注意事项


参考文章:
1、https://blog.csdn.net/qiao0809/article/details/53506008
2、https://blog.csdn.net/xiaoxiaocaizi123/article/details/79074501
3、https://blog.csdn.net/android_cyw/article/details/54632112

猜你喜欢

转载自blog.csdn.net/zeqiao/article/details/83304096