1.使用ViewPager绑定静态fragment
private TabLayout tab;
private ViewPager vp;
private ArrayList list;
private ArrayList title;
private MyFragmentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
initView();
}
private void initView() {
tab = (TabLayout) findViewById(R.id.tab);
vp = (ViewPager) findViewById(R.id.vp);
list = new ArrayList<>();
list.add(new AFragment());
list.add(new BFragment());
list.add(new CFragment());
title = new ArrayList<>();
title.add("新闻");
title.add("娱乐");
title.add("汽车");
adapter = new MyFragmentAdapter(getSupportFragmentManager(),list,title);
vp.setAdapter(adapter);
//(静态绑定)tab与vp设置页面绑定
tab.setupWithViewPager(vp);
xml
适配器
public class MyAdapter_home extends FragmentPagerAdapter {
private ArrayList list;
private ArrayList title;
public MyAdapter_home(FragmentManager fm,ArrayList list,ArrayList title) {
super(fm);
this.list=list;
this.title=title;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return title.size();
}
}
2.使用ViewPager绑定动态fragment
public class MainActivity extends AppCompatActivity {
private TabLayout tab;
private ViewPager vp;
private ArrayList list;
private MyFragmentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tab = (TabLayout) findViewById(R.id.tab);
vp = (ViewPager) findViewById(R.id.vp);
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
//创建ViewPager中Fragment
list.add(new BlankFragment("内容"+i));
//创建TabLayout中的显示标题
tab.addTab(tab.newTab().setText("标题"+i));
}
//适配器
adapter = new MyFragmentAdapter(getSupportFragmentManager(),list);
vp.setAdapter(adapter);
//相互绑定
tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {//通过选择对应的tab,切换vp的位置
@Override
public void onTabSelected(TabLayout.Tab tab) {
vp.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
vp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab));//通过vp的滑动,选择tab的位置
}
}
适配器
public class MyAdapter_home extends FragmentPagerAdapter {
private ArrayList list;
public MyAdapter_home(FragmentManager fm,ArrayList list) {
super(fm);
this.list=list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
3.TabLayout绑定ViewPager设置图片与选择器
1.代码实现
public class wan_fragment extends Fragment {
private ViewPager vp;
private TabLayout tab;
private ArrayList list;
int[] title={R.string.fragment_shou,R.string.fragment_zhi,R.string.fragment_gong,R.string.fragment_dao,R.string.fragment_xiang};
int[] zhuti={R.drawable.home,R.drawable.zhishiti,R.drawable.gong,R.drawable.dao,R.drawable.xiang};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_wan_fragment, container, false);
initView(inflate);
return inflate;
}
private void initView(View inflate) {
vp = (ViewPager) inflate.findViewById(R.id.vp);
tab = (TabLayout) inflate.findViewById(R.id.tab);
list = new ArrayList<>();
list.add(new ShouYe_Fragment());
list.add(new xiang_Fragment());
list.add(new xiang_Fragment());
list.add(new xiang_Fragment());
list.add(new xiang_Fragment());
MyAdapter_home home = new MyAdapter_home(getChildFragmentManager(), list);
vp.setAdapter(home);
tab.setupWithViewPager(vp);
for (int i = 0; i < tab.getTabCount(); i++) {
TabLayout.Tab tabAt = tab.getTabAt(i);
//TabLayout引用的一个xml布局文件
View inflate1 = LayoutInflater.from(getActivity()).inflate(R.layout.tab, null, false);
//初始化
ImageView img = inflate1.findViewById(R.id.img);
TextView tv = inflate1.findViewById(R.id.title);
img.setImageResource(zhuti[i]);
tv.setText(title[i]);
//使用
tabAt.setCustomView(inflate1);
}
}
}
适配器
public class MyAdapter_home extends FragmentPagerAdapter {
private ArrayList list;
public MyAdapter_home(FragmentManager fm,ArrayList list) {
super(fm);
this.list=list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
选择器