Generally, an activity may contain one or more fragment, which work together to form a consistent UI interface. In this case, communication between multiple fragments is very important. For example, an activity contains two fragment items, and the fragment field on the left contains a list (for example, a news topic list). When you click on a news topic, the fragment on the right shows detailed information about the news.
The following describes how to perform operations.
Project directory:
Fragment1 is on the left of the entire activity, and Fragment2 is on the right.
1. Code in fragment1.xml.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "#00FF00"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/lblFragment1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "This is fragment #1"
Android: textColor = "#000000"
Android: textSize = "25sp"/>
</LinearLayout>
2. fragment2.xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# FFFE00"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "This is fragment #2"
Android: textColor = "#000000"
Android: textSize = "25sp"/>
<Button
Android: id = "@ + id/btnGetText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "onClick"
Android: text = "Get text in Fragment #1"
Android: textColor = "#000000" type = "codeph" text = "/codeph"/>
</LinearLayout>
3. Code in main. xml.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "horizontal">
<Fragment
Android: id = "@ + id/fragment1"
Android: name = "net. learn2develop. Fragments. Fragment1"
Android: layout_width = "0px"
Android: layout_height = "match_parent"
Android: layout_weight = "1"/>
<Fragment
Android: id = "@ + id/fragment2"
Android: name = "net. learn2develop. Fragments. Fragment2"
Android: layout_width = "0px"
Android: layout_height = "match_parent"
Android: layout_weight = "1"/>
</LinearLayout>
4. Code in FragmentsActivity. java.
Package net. learn2develop. Fragments;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. TextView;
Import android. widget. Toast;
Public class FragmentsActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Public void onClick (View v ){
TextView lbl = (TextView) findViewById (R. id. lblFragment1 );
Toast. makeText (this, lbl. getText (), Toast. LENGTH_SHORT). show ();
}
}
5. Code in Fragment2.java.
Package net. learn2develop. Fragments;
Import android. app. Fragment;
Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. Toast;
Public class Fragment2 extends Fragment {
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
// --- Inflate the layout for this fragment ---
Return inflater. inflate (R. layout. fragment2, container, false );
}
@ Override
Public void onStart (){
Super. onStart ();
// --- Button view ---
Button btnGetText = (Button) getActivity ()
. FindViewById (R. id. btnGetText );
BtnGetText. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// Here is the key. The getActivity () method returns the activity instance where the fragment is located, and the components in the activity instance can be obtained through the activity instance. Other components are easy. Www.2cto.com
TextView lbl = (TextView) getActivity (). findViewById (
R. id. lblFragment1 );
Toast. makeText (getActivity (), lbl. getText (), Toast. LENGTH_SHORT)
. Show ();
}
});
}
}
6. debug. :
Click "Get text in Fragment #1" on the right side. A prompt is displayed.
From manoel's column