android BottomNavigationView 使用问题
程序员文章站
2022-06-23 08:44:20
这段时间在做一个高速的手持机项目,在使用Google自带BottomNavigationView时发现了一个,在重写了OnNavigationItemSelectedListener了之后换不了fragment,在网上查询了半天也都是配合ViewPager来使用的,毕竟迷茫,如何不使用ViewPager切换fragment代码如下:package com.handset.lane.activity;import android.annotation.SuppressLint;import a...
这段时间在做一个高速的手持机项目,在使用Google自带BottomNavigationView时发现了一个,在重写了OnNavigationItemSelectedListener了之后换不了fragment,在网上查询了半天也都是配合ViewPager来使用的,毕竟迷茫,如何不使用ViewPager切换fragment
代码如下:
package com.handset.lane.activity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.handset.lane.R;
import com.handset.lane.base.BaseActivity;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;
public class CardInfoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_info);
this.getSupportActionBar().hide();
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
TextView title = findViewById(R.id.tv_title);
title.setText(R.string.cpc);
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
title.setText(R.string.cpc);
return true;
case R.id.navigation_dashboard:
title.setText(R.string.obu);
return true;
}
return false;
}
});
}
}
本文地址:https://blog.csdn.net/chengmuzhe2690/article/details/110291005