展示效果
TabLayout使用
添加依赖
compile 'com.android.support:design:23.4.0'
布局
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/tablayout">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager">
**通常我们都搭配ViewPager+Fragment使用。
页面用Fragment,放了一个小图标。
然后是ViewPager的适配器:**
public class FragmentAdapter extends FragmentPagerAdapter {
private String [] title = {"one","two","three","four"};
private List<Fragment> fragmentList;
public FragmentAdapter(FragmentManager fm,List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ContentFragment fragment1 = new ContentFragment();
ContentFragment fragment2 = new ContentFragment();
ContentFragment fragment3 = new ContentFragment();
ContentFragment fragment4 = new ContentFragment();
fragments.add(fragment1);
fragments.add(fragment2);
fragments.add(fragment3);
fragments.add(fragment4);
adapter = new FragmentAdapter(getSupportFragmentManager(),fragments);
viewpager.setAdapter(adapter);
tablayout.setupWithViewPager(viewpager);
}