Implementation of simple communication between fragment and fragment
This article uses the most basic fragment knowledge for beginners only. It also provides basic preparation for related knowledge in the future. It would be a waste of minutes for you to take a look.
For more information about fragment APIs, click here to view the basic introduction to Android Fragment.
First, let's look at the interface
The TextView on the left changes according to the button on the right.
The following code is introduced:
1. Create fragment1.xml in layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:orientation="vertical" > <TextView android:id="@+id/fragment_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 1" android:textColor="#000000" android:textSize="25sp" /></LinearLayout>
We can see that there is only one TextView.
2. Create fragment2.xml in layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 2" android:textColor="#000000" android:textSize="25sp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 3" /></LinearLayout>
Here are three buttons.
3. Create a class Fragment1 to inherit Fragment
package lgx.fram.framents;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment1, container, false);}}
Override the onCreateView () method. Here return inflater. inflate (R. layout. fragment1, container, false); this sentence is important.
4. Create a class Fragment2 to inherit Fragment
package lgx.fram.framents;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;public class Fragment2 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment2, container, false);}TextView textview;Button button, button2, button3;@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);button = (Button) getActivity().findViewById(R.id.button);button2 = (Button) getActivity().findViewById(R.id.button2);button3 = (Button) getActivity().findViewById(R.id.button3);textview = (TextView) getActivity().findViewById(R.id.fragment_text);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {textview.setText(button.getText());}});button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {textview.setText(button2.getText());}});button3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {textview.setText(button3.getText());}});}}
Button = (Button) getActivity (). findViewById (R. id. button); this method is used to obtain the control above fragment.
5. The code in activity_fragment.xml looks like this.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <fragment android:id="@+id/fragment1" android:name="lgx.fram.framents.Fragment1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="lgx.fram.framents.Fragment2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /></LinearLayout>
Note: In the fragment control, android: name = "" indicates the absolute path of your Fragment class (which is short-circuited by the brain suddenly ??), Id indicates fragment.
6. FragmentActivity is the simplest, just setContentView, and no other changes are made. See the following:
package lgx.fram.framents;import android.app.Activity;import android.os.Bundle;public class FragmentActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_fragment);}}
Here, my entire small application is finished. Here, the Fragment is added to the Activity through the layout file, and another way is to add the Fragment to the Activity through programming. Here I will briefly describe
The above 1, 2, 3, 4 do not need to be moved.
In step 5th, the code in activity_fragment.xml is changed to the following
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > </LinearLayout>
You will find that two Fragment items are removed from my knowledge, and the id is added to the entire LinearLayout.
The comments in step 1 have been clearly written:
Package lgx. fram. framents; import android. OS. bundle; import android. app. activity; import android. view. display; import android. view. menu;/***** @ author lenovo add Fragment dynamically in four steps: * 1. get the FragmentManager, which can be obtained directly through getFragmentManager in the Activity. ** 2. Start a transaction by calling the beginTransaction method. ** 3. Add the Fragment to the container, which is generally implemented using the replace method. You need to input the container id and Fragment instance. ** 4. Submit the transaction and call the commit method to submit the transaction. */Public class FragmentActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_fragment); Display display = getWindowManager (). getdefadisplay display (); if (display. getWidth ()> display. getHeight () {Fragment1 fragment1 = new Fragment1 (); getFragmentManager (). beginTransaction (). replace (R. id. main_layout, fragment1 ). commit ();} else {Fragment2 fragment2 = new Fragment2 (); getFragmentManager (). beginTransaction (). replace (R. id. main_layout, fragment2 ). commit ();}}}
This Code indicates that different Fragment entries are displayed on the horizontal and vertical screens. For a simulated machine test, press Ctrl + F11.
Here I use the replace method, and another method is add... I will not go into details here. Please go here to see the basic introduction to Android Fragment.
Finished...