目前app内容的整体结构基本都是底部几个按钮来切换内容界面这种方式,要想实现这种有很多方案,下面介绍一种比较常规实现的方案。
通过RadioGroup和Viewpage,viewpage中添加Fragment。
首先是xml中布局设置
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/home_vp" android:layout_weight="6" android:layout_width="match_parent" android:layout_height="0dp"></android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/bottom_rg" android:paddingBottom="0dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/home_bt" android:layout_weight="1" android:drawableTop="@drawable/home_select" android:button="@null" android:checked="true" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/img_bt" android:layout_weight="1" android:button="@null" android:layout_width="0dp" android:drawableTop="@drawable/img_select" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/movie_bt" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/movie_select" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/person_bt" android:drawableTop="@drawable/person_select" android:layout_weight="1" android:button="@null" android:layout_width="0dp" android:layout_height="wrap_content" /> </RadioGroup> </LinearLayout>
布局比较简单,可以按照自己的需求设置。
接着在activity中初始化控件
private void initView() { home_vp= findViewById(R.id.home_vp); home_bt= findViewById(R.id.home_bt); img_bt= findViewById(R.id.img_bt); movie_bt= findViewById(R.id.movie_bt); person_bt= findViewById(R.id.person_bt); bottom_rg=findViewById(R.id.bottom_rg); }
然后是实例化每个按钮对应界面的Fragment
private void initData() { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.home_vp,new HomeFragment()) .commit(); ArrayList<Fragment> list = new ArrayList<Fragment>(); if(homeFragment ==null) { homeFragment = new HomeFragment(); list.add(homeFragment); } if(imgFragment ==null) { imgFragment = new ImgFragment(); list.add(imgFragment); } if(movieFragment ==null) { movieFragment = new MovieFragment(); list.add(movieFragment); } if(personFragment ==null) { personFragment = new PersonFragment(); list.add(personFragment); } home_vp.setAdapter(new MyViewPageAdapter(fragmentManager,list)); }
每个Fragment界面可以根据自己的需求进行设置
public class MovieFragment extends BaseFragment { public MovieFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_movie, container, false); } }
因为Fragment是放在ViewPage 中的,所以要设置ViewPage的适配器
public class MyViewPageAdapter extends FragmentPagerAdapter { ArrayList<Fragment> list; public MyViewPageAdapter(FragmentManager fm, ArrayList<Fragment> list) { super(fm); this.list=list; } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { } }
这个适配器也可以根据自己的需要进行一些特殊的设置,当前只是最简单的实现。 现在Fragment已经添加到ViewPage中啦,但还要和下部的按钮进行联动。
首先 设置ViewPage滑动和按钮的联动 ,设置ViewPage的滑动监听。
home_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0 : bottom_rg.check(R.id.home_bt); break; case 1 : bottom_rg.check(R.id.img_bt); break; case 2 : bottom_rg.check(R.id.movie_bt); break; case 3 : bottom_rg.check(R.id.person_bt); break; } } @Override public void onPageScrollStateChanged(int state) { } });
然后在设置RadioGroup的点击ViewPage 对应显示页面的监听
bottom_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.home_bt : home_vp.setCurrentItem(0,true); break; case R.id.img_bt : home_vp.setCurrentItem(1,true); break; case R.id.movie_bt : home_vp.setCurrentItem(2,true); break; case R.id.person_bt : home_vp.setCurrentItem(3,true); break; } } });
现在整体界面基本已经搭建好啦,但是可能有的小伙伴们在,页面滑到动时,前面加载的数据会消失,这个时候注意要在ViewPage的适配器中将destroyItem中继承的父类super方法删除。原因可以参考我的另一篇博客https://blog.csdn.net/weixin_42171638/article/details/84770944