新建两个Fragment,一个Fragment1一个Fragment2
Fragment1
package com.example.myfragment; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import java.security.PrivateKey; public class BlankFragment extends Fragment { private View root; private TextView textView; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (root == null){ // Inflate the layout for this fragment root = inflater.inflate(R.layout.fragment_blank, container, false); } textView = root.findViewById(R.id.textview1); button = root.findViewById(R.id.btn1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView.setText("我很好,谢谢"); } }); return root; } }
Fragment2
package com.example.myfragment; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class BlankFragment2 extends Fragment { private View root; private TextView textView; private Button button; public BlankFragment2() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (root == null){ root = inflater.inflate(R.layout.fragment_blank2, container, false); } //这些赋值放到if的下面奥,要不会报错 textView = root.findViewById(R.id.textview2); button = root.findViewById(R.id.btn2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView.setText("我还是很好"); } }); // Inflate the layout for this fragment return root; } }
MainActivity啥也没写
Fragment1.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".BlankFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_blank_fragment" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="你好吗"/> </FrameLayout>
Fragment2.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BlankFragment2"> <!-- TODO: Update blank fragment layout --> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="再问一遍" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="你还好吗"/> </FrameLayout>
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <fragment android:id="@+id/fragment1" android:name="com.example.myfragment.BlankFragment" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <fragment android:id="@+id/fragment2" android:name="com.example.myfragment.BlankFragment2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
效果如下: