实现过程
1.在build.gradle中添加依赖,例如:
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:design:23.4.0'
2.然后在主布局文件activity_main.xml中添加布局控件:
<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"
>
<LinearLayout
android:id="@+id/home_tab_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_color"
android:gravity="center_vertical">
<ImageView
android:id="@+id/home_tab_search_img"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="4dp"
android:src="@mipmap/search"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabTextColor="@color/white_color"
app:tabSelectedTextColor="#E6E6FA"
app:tabIndicatorColor="@color/white_color"/>
<ImageView
android:id="@+id/home_tab_rank_img"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="4dp"
android:src="@mipmap/icon_toplist"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_live_score"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3.接下来就我们集添加适配器
public class MyPageAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
private List<String> titles;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
super(fm);
this.fragments = fragments;
this.titles = titles;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size() != 0 ? fragments.size() : 0;
}
@Override
public CharSequence getPageTitle(int position) {
if (titles != null)
return titles.get(position);
else
return null;
}
}
4.在HomeFragment中使用
@BindView(R.id.tab_layout)
TabLayout tabLayout;
@BindView(R.id.vp_live_score)
ViewPager live_view;
private List<String> mTitles;
private List<Fragment> fragments;
private MyPageAdapter pagerAdapter;
@Override
protected int getLayout() {
return R.layout.fragment_home;
}
@Override
protected void initView() {
}
@Override
protected void initData() {
fragments = new ArrayList<>();
mTitles=new ArrayList<>();
mTitles.add("关注");
mTitles.add("热门");
mTitles.add("最新");
mTitles.add("附近");
fragments.add(new AttentionFragment());
fragments.add(new HotFragment());
fragments.add(new NewestFragment());
fragments.add(new NearbyFragment());
pagerAdapter = new MyPageAdapter(getChildFragmentManager(), fragments,mTitles);
live_view.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(live_view);
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if(!hidden){
}
}
5.设置当前位置
//设置默认选择tab
tabLayout.setCurrentTab(1);
live_view.setCurrentItem(1);