导航组件 - 导航抽屉无法导航



我刚刚用一个新项目实现了导航,但我的导航抽屉并没有膨胀每个导航片段,它只是在我单击选项的任何地方显示第一个

public class DisplayScreen2 extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_screen2);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_perfil, R.id.nav_clima, R.id.nav_mapa,
R.id.nav_config, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}

显示的唯一片段是这个

public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
public HomeViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is home fragment");
}
public LiveData<String> getText() {
return mText;
}
}

导航抽屉的其余片段在我单击它们后没有显示,我真的不知道为什么会发生这种情况,以及我是否需要在主页片段中设置一些东西才能进入其他片段

谢谢

只要确保drawer_menu.xml中项目的 id 和navigation.xml中片段的 id 匹配即可。 下面是一个示例:

drawer_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/fragment1"
android:title="Fragment 1"/>
<item android:id="@+id/fragment2"
android:title="Fragment 2"/>
<item android:id="@+id/fragment3"
android:title="Fragment 3"/>
</menu>

导航.xml

<navigation 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"
app:startDestination="@id/fragment1">
<fragment
android:id="@+id/fragment1" //this id should match the corresponding item in drawer_menu.xml
android:name="Fragment1"
android:label="@string/fragment_1_title"
tools:layout="@layout/fragment1" />
<fragment
android:id="@+id/fragment2" //this id should match the corresponding item in drawer_menu.xml
android:name="Fragment2"
android:label="@string/fragment_2_title"
tools:layout="@layout/fragment2" />
<fragment
android:id="@+id/fragment3" //this id should match the corresponding item in drawer_menu.xml
android:name="Fragment3"
android:label="@string/fragment_3_title"
tools:layout="@layout/fragment3" />
</navigation>

解决方案是添加这个

navigationView.bringToFront();
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_gallery:
Toast.makeText(DisplayScreen.this, "Gallery", Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawers();
return false;
}

});

为了导航,这应该使用默认的导航抽屉模板实现

相关内容

  • 没有找到相关文章

最新更新