主要实现底部导航栏,点击底部按钮进行页面切换,同时进行按钮变化
本篇文章使用ViewPager+Fragment实现效果
布局文件
viewPager和Fragment
其中RadioButton的背景需要写选择器,一个单选按钮对应一个选择器,这里只贴出一个选择器
因为格式都是一样的,只是图片不同而已
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/home1"></item> <item android:drawable="@drawable/home0"></item> </selector>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" tools:context=".View.activitys.HomeActivity" xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9.5"></android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/group" android:paddingRight="10dp" android:paddingLeft="10dp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.8" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_home" android:layout_width="0dp" android:checked="true" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/select1" android:button="@null" /> <RadioButton android:id="@+id/rb_classify" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/select2" android:button="@null" /> <RadioButton android:id="@+id/rb_find" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/select3" android:button="@null" /> <RadioButton android:id="@+id/rb_car" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/select4" android:button="@null" /> <RadioButton android:id="@+id/rb_mine" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/select5" android:button="@null" /> </RadioGroup> </LinearLayout>
Activity中
将Fragment写入集合中
Fragment的数量取决于你页面的数量
RadioButton的数量取决于你Fragment的数量
ViewPager+Fragment的方法
viewPager = findViewById(R.id.viewPager); group = findViewById(R.id.group); //数据源Fragment initFragment(); //ViewPager的适配器 initAdapter(); //ViewPager的监听 initViewPagerListener(); //RadioGroup initRadioGroup();
private void initRadioGroup() { group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.rb_home: viewPager.setCurrentItem(0); break; case R.id.rb_classify: viewPager.setCurrentItem(1); break; case R.id.rb_find: viewPager.setCurrentItem(2); break; case R.id.rb_car: viewPager.setCurrentItem(3); break; case R.id.rb_mine: viewPager.setCurrentItem(4); break; } } }); } private void initViewPagerListener() { viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: group.check(R.id.rb_home); break; case 1: group.check(R.id.rb_classify); break; case 2: group.check(R.id.rb_find); break; case 3: group.check(R.id.rb_car); break; case 4: group.check(R.id.rb_mine); break; } } @Override public void onPageScrollStateChanged(int state) { } }); } private void initAdapter() { viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }); } private void initFragment() { fragments = new ArrayList<>(); fragments.add(new HomeFragment()); fragments.add(new ClassifyFragment()); fragments.add(new FindFragment()); fragments.add(new ShoppingCarFragment()); fragments.add(new MineFragment()); }