Android

TabLayout +ViewPager+Fragment 组合

2018-11-08  本文已影响9人  Ello_Orld

1. tabLayout的使用

   <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize">

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab1" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab2"/>

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab3" />
        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab4" />

    </android.support.design.widget.TabLayout>
 TabLayout tabLayout = findViewById(R.id.tab);
        TabLayout.Tab tab = tabLayout.newTab();
        tab.setText("Tab1");
        //添加tab
        tabLayout.addTab(tab);
        //删除tab
        tabLayout.removeTab(tab);
        tabLayout.removeTabAt(0);
        //删除全部tab
        tabLayout.removeAllTabs();

效果图:


image.png
  1. 显示模式
    可滑动: app:tabMode="scrollable"
    固定: app:tabMode="fixed"

  2. 指示器选项
    指示器高度 :app:tabIndicatorHeight="10dp"
    指示器颜色 :app:tabIndicatorColor="@color/colorPrimary"

  3. 文字选项
    选择的Tab的文字颜色:app:tabSelectedTextColor="#ffffff"
    未选择的Tab文字颜色:app:tabTextColor="#000000"

  4. 文字样式:
    app:tabTextAppearance="@style/Base.TextAppearance.AppCompat.Large"

  5. 背景设置(两者没什么区别)
    android:background="@color/colorAccent"
    app:tabBackground="@color/colorPrimary"

  6. 标签距离
    app:tabPaddingStart="10dp"
    app:tabPaddingBottom="10dp"
    app:tabPadding="10dp"
    app:tabPaddingEnd="10dp"
    app:tabPaddingTop="10dp"

  7. 对齐方式
    居中显示:app:tabGravity="center"
    填充:app:tabGravity="fill"
    偏移:app:tabContentStart="200dp"(从左边开始偏移距离, 必须是可滑动的模式 scrollable)

  8. 标签宽度限制
    最大宽度:app:tabMaxWidth="50dp"
    最小宽度:app:tabMinWidth="100dp"

2. TabLayout + ViewPager + Fragment

示例:
tabLayout包含4个tab,每个tab对应一个单独的fragment
fragment代码(布局中仅包含一个textView)

public class MainFragment extends Fragment {
    TextView mTextView;
    public static MainFragment newInstance(String title) {
        MainFragment mainFragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        mainFragment.setArguments(bundle);
        return mainFragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view, container, false);
        mTextView = view.findViewById(R.id.textView);
        mTextView.setText((CharSequence) getArguments().get("title"));
        return view;
    }
}

添加viewPager的adapter

class ViewPagerAdapter extends FragmentStatePagerAdapter{

        List<Fragment> mFragments;
        String[] mTitles;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, String[] titles) {
            super(fm);
            mFragments = fragments;
            mTitles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

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

activity中:

mTab = findViewById(R.id.tab);
        mViewPager = findViewById(R.id.viewpager);

        String[] titles = {"TAB1", "TAB2", "TAB3", "TAB4"};

        Fragment fragment1 = MainFragment.newInstance(titles[0]);
        Fragment fragment2 = MainFragment.newInstance(titles[1]);
        Fragment fragment3 = MainFragment.newInstance(titles[2]);
        Fragment fragment4 = MainFragment.newInstance(titles[3]);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        fragments.add(fragment4);

        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments, titles);
        mViewPager.setAdapter(adapter);
        mTab.setupWithViewPager(mViewPager);
Screenrecorder-2018-11-08-17-26-05-755.gif

2. 使用图标的tabItem

tabItem有icon属性,可以在布局中直接设置:

 <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:tabIndicatorHeight="0dp"
            app:tabMode="fixed" >
            <android.support.design.widget.TabItem
                android:icon="@drawable/tab_item_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <android.support.design.widget.TabItem
                android:icon="@drawable/tab_item_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <android.support.design.widget.TabItem
                android:icon="@drawable/tab_item_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </android.support.design.widget.TabLayout>

但是这样有个问题,图标太小,没找到可以调整图标大小的办法。显示效果不好。
这里我们可以使用自定义view 的方式。
修改布局:去掉tabItem,改为动态添加。

    <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:tabIndicatorHeight="0dp"
            app:tabMode="fixed" />

添加自定义布局:tabitem_icon_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <ImageView
        android:src="@drawable/tab_item_icon"
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

图标资源:tab_item_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_home_blue_24dp" android:state_selected="true" />
    <item android:drawable="@drawable/ic_home_black_24dp" />
</selector>

activity中动态添加:

private void initTab() {
        //添加底部tab
        for (int i = 0; i < 4; i++) {
            View view = getLayoutInflater().inflate(R.layout.tabitem_icon_layout, null);
            view.findViewById(R.id.icon).setBackgroundResource(R.drawable.tab_item_icon);
            mTabLayout.addTab(mTabLayout.newTab().setCustomView(view));
        }
    }

这样就达到了我们的效果


image.png

问题:与viewPager的关联
如果用上面提到的方法:
mTabLayout.setupWithViewPager(mViewPager);
我们设置的自定义布局就没有了,tabItem都变成文字了。
可以采用以下方式进行关联:

 mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
上一篇下一篇

猜你喜欢

热点阅读