gpt4 book ai didi

java - NavigationComponents - 抽屉导航无法导航

转载 作者:行者123 更新时间:2023-12-01 23:21:44 26 4
gpt4 key购买 nike

我刚刚使用一个新项目实现了导航,但我的抽屉导航并没有膨胀每个导航 fragment ,它只是在我单击选项时显示第一个导航 fragment

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();
}
}

唯一显示的 fragment 是这个

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;
}
}

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

谢谢

最佳答案

只需确保 drawer_menu.xml 中的项目 ID 与 navigation.xml 中的 fragment ID 匹配即可。这是一个例子:

抽屉菜单.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>

关于java - NavigationComponents - 抽屉导航无法导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58326162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com