RadioGroup + viewpage + fragment 组合显示导航栏
1、首先主界面的布局控件就是RadioGroup + viewpage
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.mmsx.MainActivity"><androidx.viewpager.widget.ViewPagerandroid:id="@+id/main_viewpager"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1" /><RadioGroupandroid:id="@+id/rg"android:layout_width="match_parent"android:layout_height="60dp"android:layout_alignParentBottom="true"android:background="#FDFDFD"android:gravity="center"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rb_wallet"style="@style/main_rb_style"android:drawableTop="@drawable/button_style_1" /><RadioButtonandroid:id="@+id/rb_treasure"style="@style/main_rb_style"android:drawableTop="@drawable/button_style_2" /><RadioButtonandroid:id="@+id/rb_home"style="@style/main_rb_style"android:drawableTop="@drawable/button_style_3" /><RadioButtonandroid:id="@+id/rb_otc"style="@style/main_rb_style"android:drawableTop="@drawable/button_style_4" />/></RadioGroup>
</LinearLayout>
这样式可以统一定义好style样式,直接调用
图标的样式
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/home" android:state_checked="true" />
<item android:drawable="@mipmap/home_1" android:state_checked="false" />
</selector>
2、MainActivity实现
首先写一个fragment适配器
public class MainFragmentPagerAdapter extends FragmentPagerAdapter {List<Fragment> mFragmentList;public MainFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {super(fm);mFragmentList=list;}@Overridepublic Fragment getItem(int i) {return mFragmentList.get(i);}@Overridepublic int getCount() {return mFragmentList!=null?mFragmentList.size():0;}@Overridepublic void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
// super.destroyItem(container, position, object);}
}
MainActivity代码实现,实现滑动切换,点击导航栏切换。
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {List<Fragment> fragmentList = new ArrayList<>();MainFragmentPagerAdapter adapter;ViewPager viewPager;RadioGroup rg;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化控件initView();//绑定RadioButtoninitViewPager();}private void initView() {viewPager = findViewById(R.id.main_viewpager);rg = findViewById(R.id.rg);rg.setOnCheckedChangeListener(this);rg.getChildAt(0).performClick();}private void initViewPager() {//添加碎片fragmentList.add(new HomeFragment());fragmentList.add(new MessageFragment());fragmentList.add(new CustomFragment());fragmentList.add(new LoginFragment());adapter = new MainFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);viewPager.setAdapter(adapter);viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int i, float v, int i1) {}@Overridepublic void onPageSelected(int i) {switch (i) {case 0:rg.check(R.id.rb_wallet);break;case 1:rg.check(R.id.rb_treasure);break;case 2:rg.check(R.id.rb_home);break;case 3:rg.check(R.id.rb_otc);break;}}@Overridepublic void onPageScrollStateChanged(int i) {}});}@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int checkedId) {switch (checkedId) {case R.id.rb_wallet:viewPager.setCurrentItem(0);break;case R.id.rb_treasure:viewPager.setCurrentItem(1);break;case R.id.rb_home:viewPager.setCurrentItem(2);break;case R.id.rb_otc:viewPager.setCurrentItem(3);break;}}
}
创建四个fragment代码这里就不贴了。会有点多。四个fragment还有功能实现。
看一下项目结构图
具体实现的界面如下