1. 涉及到的技术点
- 底部导航控件BottomNavigationView的使用
- 相对布局RelativeLayout的使用
- BottomNavigationView的监听事件处理
- Fragment的使用
2.具体代码实现过程
- 编写activity_main.xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/content"
android:layout_above="@id/main_bottom_nv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_bottom_nv"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
app:labelVisibilityMode="labeled"
android:background="@color/white"
android:layout_height="wrap_content"
app:menu="@menu/navigation" />
</RelativeLayout>
- 在res下新建menu文件夹,然后在menu下新建navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@drawable/baseline_home_24"
android:title="首页" />
<item
android:id="@+id/car"
android:icon="@drawable/baseline_shopping_cart_24"
android:title="购物车" />
<item
android:id="@+id/order"
android:icon="@drawable/baseline_reorder_24"
android:title="订单" />
<item
android:id="@+id/mine"
android:icon="@drawable/ic_baseline_supervisor_account_24"
android:title="我的" />
</menu>
- 在MainActivity中实现代码
public class UserMainActivity extends MainActivity {
//创建4个 Fragment
private HomeFragment mHomeFragment;
private CardFragment mCardFragment;
private OrderFragment mOrderFragment;
private MineFragment mMineFragment;
private BottomNavigationView main_bottom_nv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
main_bottom_nv = findViewById(R.id.main_bottom_nv)
//main_bottom_nv tab点击切换
main_bottom_nv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.home) {
selectedFragment(0);
} else if (item.getItemId() == R.id.car) {
selectedFragment(1);
} else if (item.getItemId() == R.id.order) {
selectedFragment(2);
} else if (item.getItemId() == R.id.mine) {
selectedFragment(3);
}
return true;
}
});
//默认首页选中
selectedFragment(0);
}
public void selectedFragment(int position) {
//获取fragmentManager管理器
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
hideFragment(transaction);
if (position == 0) {
if (mHomeFragment == null) {
mHomeFragment = new HomeFragment();
transaction.add(R.id.content, mHomeFragment);
} else {
transaction.show(mHomeFragment);
}
} else if (position == 1) {
if (mCardFragment == null) {
mCardFragment = new CardFragment();
transaction.add(R.id.content, mCardFragment);
} else {
transaction.show(mCardFragment);
}
} else if (position == 2) {
if (mOrderFragment == null) {
mOrderFragment = new OrderFragment();
transaction.add(R.id.content, mOrderFragment);
} else {
transaction.show(mOrderFragment);
}
} else {
if (mMineFragment == null) {
mMineFragment = new MineFragment();
transaction.add(R.id.content, mMineFragment);
} else {
transaction.show(mMineFragment);
}
}
//注意:这句话一定不能少!!!!!!
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction) {
if (mHomeFragment != null) {
transaction.hide(mHomeFragment);
}
if (mMineFragment != null) {
transaction.hide(mMineFragment);
}
if (mCardFragment != null) {
transaction.hide(mCardFragment);
}
if (mOrderFragment != null) {
transaction.hide(mOrderFragment);
}
}
}
在MainActivity中,使用到了4个Fragment页面,那么为什么要使用Fragment呢?
在实际开发过程中,类似于底部导航页面之间的切换,基本上采用Fragment去实现