![](/img/trans.png)
[英]Android Drawer - Navigating to Multiple Fragments using Navigation.findNavController()
[英]NavigationComponents - Navigation drawer not navigating
我剛剛用一個新項目實現了導航,但是我的導航抽屜並沒有膨脹每個導航片段,它只是在我單擊的任何選項處顯示第一個
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;
}
}
導航抽屜的片段的rest在我點擊后沒有顯示出來,我真的不知道為什么會這樣,如果我需要在主頁片段中設置一些東西才能進入其他片段
謝謝
只需確保您的drawer_menu.xml
中的項目ID 和navigation.xml
中片段的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.