RadioGroup + viewpage + fragment 组合显示导航栏
1、首先主界面的布局控件就是RadioGroup + viewpage
XML
<?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.ViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioGroup
android: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">
<RadioButton
android:id="@+id/rb_wallet"
style="@style/main_rb_style"
android:drawableTop="@drawable/button_style_1" />
<RadioButton
android:id="@+id/rb_treasure"
style="@style/main_rb_style"
android:drawableTop="@drawable/button_style_2" />
<RadioButton
android:id="@+id/rb_home"
style="@style/main_rb_style"
android:drawableTop="@drawable/button_style_3" />
<RadioButton
android:id="@+id/rb_otc"
style="@style/main_rb_style"
android:drawableTop="@drawable/button_style_4" />
/>
</RadioGroup>
</LinearLayout>
这样式可以统一定义好style样式,直接调用
图标的样式
XML
<?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适配器
java
public class MainFragmentPagerAdapter extends FragmentPagerAdapter {
List<Fragment> mFragmentList;
public MainFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
mFragmentList=list;
}
@Override
public Fragment getItem(int i) {
return mFragmentList.get(i);
}
@Override
public int getCount() {
return mFragmentList!=null?mFragmentList.size():0;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
// super.destroyItem(container, position, object);
}
}
MainActivity代码实现,实现滑动切换,点击导航栏切换。
java
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
List<Fragment> fragmentList = new ArrayList<>();
MainFragmentPagerAdapter adapter;
ViewPager viewPager;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//绑定RadioButton
initViewPager();
}
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() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public 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;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
@Override
public 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还有功能实现。
看一下项目结构图
具体实现的界面如下