参考这篇代码
首先添加两个按键先,记得这个id号,后面是有调用到的
代码如下
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" />
<item
android:id="@+id/navigation_new"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="button4_fragment" />
<item
android:id="@+id/button5"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="button5" />
</menu>
在这里添加fragment声明。注意,这个id号要跟“menu”里面的id号要相同,name要跟Java代码的fragment相同,layout名是xml文件的layout文件,反正要一一对应
代码如下
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.example.myapplication3.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.myapplication3.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_notifications"
android:name="com.example.myapplication3.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
<fragment
android:id="@+id/navigation_new"
android:name="com.example.myapplication3.ui.button4.button4Fragment"
android:label="the new"
tools:layout="@layout/button4_fragment" />
<fragment
android:id="@+id/button5"
android:name="com.example.myapplication3.ui.button5.button5Fragment"
android:label="the new"
tools:layout="@layout/button4_fragment" />
</navigation>
然后在ui文件夹下新建一个文件夹命名为myNavagation,在里面new一个fragment->fragment with viewmodel
然后就会自动出现三个文件,两个Java文件,一个xml文件
button4Fragment代码
package com.example.myapplication3.ui.button4;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.myapplication3.R;
import androidx.lifecycle.ViewModelProviders;
import android.widget.TextView;
import androidx.lifecycle.Observer;
public class button4Fragment extends Fragment {
// private Button4ViewModel mViewModel;
//
// public static button4Fragment newInstance() {
// return new button4Fragment();
// }
//
// @Override
// public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
// @Nullable Bundle savedInstanceState) {
// return inflater.inflate(R.layout.button4_fragment, container, false);
// }
//
// @Override
// public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// super.onActivityCreated(savedInstanceState);
// mViewModel = new ViewModelProvider(this).get(Button4ViewModel.class);
// // TODO: Use the ViewModel
// }
private Button4ViewModel newViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
newViewModel =
ViewModelProviders.of(this).get(Button4ViewModel.class);
//上面的方法已经过时,但是不妨碍我们使用它,可以以后再修改,现在主要是入个门
View root = inflater.inflate(R.layout.button4_fragment, container, false);
final TextView textView = root.findViewById(R.id.button41);
//下面代码中observe()方法里的this,可能在IDE里是有红色波浪线的,但是却是可以编译的。
//也可以直接把this改成getViewLifecycleOwner() 就不会报错了
newViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
Button4ViewModel代码
package com.example.myapplication3.ui.button4;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class Button4ViewModel extends ViewModel {
// TODO: Implement the ViewModel
private MutableLiveData<String> mText;
public Button4ViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is dashboard fragment");
}
public LiveData<String> getText() {
return mText;
}
}
button4_fragment.xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/button41"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:text="这个是新建的"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在MainActivity里面加上添加的那两个id号
如下图,id号要跟menu和mobile_navigation的id号要一致