通常情況下,一個activity可能包含一個或多個fragment,它們協同工作,組成一個連貫的UI界面。在這種情況下,多個fragments之間的通信顯得就很重要瞭。舉個例子,一個activity包含左右兩個fragment,左側的fragment包含瞭一個列表(比如新聞題目列表),當點擊每個新聞題目的時候,右側的fragment就會顯示這條新聞的詳盡信息。
下面展示如何進行操作。
工程目錄:
Fragment1在整個activity的左側,Fragment2在右側。
1.fragment1.xml中的代碼。
[java] <?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>
<?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
[java] <?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" />
</LinearLayout>
<?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" />
</LinearLayout>3.main.xml中的代碼。
[java] <?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>
<?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.FragmentsActivity.java中的代碼。
[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();
}
}
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.Fragment2.java中的代碼。
[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) {
// 這裡是關鍵,getActivity()方法返回這個fragment所在的activity實例,通過activity實例可以獲取在其中的組件,其他就很簡單瞭。
TextView lbl = (TextView) getActivity().findViewById(
R.id.lblFragment1);
Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT)
.show();
}
});
}
}
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) {
// 這裡是關鍵,getActivity()方法返回這個fragment所在的activity實例,通過activity實例可以獲取在其中的組件,其他就很簡單瞭。
TextView lbl = (TextView) getActivity().findViewById(
R.id.lblFragment1);
Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT)
.show();
}
});
}
}6.調試。
效果圖:
點擊右邊的“Get text in Fragment #1”按鈕,將彈出一個提示。
摘自 horsttnann的專欄