①布局
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width=“match_parent”
android:layout_height=“100dp”></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width=“match_parent”
android:layout_height=“0dp”
android:layout_weight=“1”></android.support.v4.view.ViewPager>
②TabLayout+ViewPager
package mmy.example.com.yklx1.fragment;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import java.util.ArrayList;
import mmy.example.com.yklx1.R;
import mmy.example.com.yklx1.base.BaseFragment;
import mmy.example.com.yklx1.frag.Frag1;
import mmy.example.com.yklx1.frag.Frag2;
import mmy.example.com.yklx1.frag.Frag3;
import mmy.example.com.yklx1.frag.Frag4;
import mmy.example.com.yklx1.frag.Frag5;
public class Fragment1 extends BaseFragment {
private TabLayout tabLayout;
private ViewPager pager;
private ArrayList strings;
private ArrayList list;
private Fragment1.base base;
@Override
protected void initView(View view) {
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
pager = (ViewPager) view.findViewById(R.id.pager);
strings = new ArrayList<>();
list = new ArrayList<>();
list.add(new Frag1());
list.add(new Frag2());
list.add(new Frag3());
list.add(new Frag4());
list.add(new Frag5());
for (int i = 0; i < 5; i++) {
strings.add(" "+"首页"+" ");
strings.add(" "+"发现"+" ");
strings.add(" "+"关注"+" ");
strings.add(" "+"推荐"+" ");
strings.add(" "+"我的"+" ");
}
base = new base(getChildFragmentManager());
pager.setAdapter(base);
tabLayout.setupWithViewPager(pager);
}
@Override
protected void initData() {
}
@Override
protected int getLayoutId() {
return R.layout.fragment1;
}
class base extends FragmentPagerAdapter{
public base(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return strings.get(position);
}
}
//依赖 design