Android fragments实例之间的通信

来源:互联网 发布:爱国 知乎 编辑:程序博客网 时间:2024/05/17 04:16
http://blog.csdn.net/manoel/article/details/7578962

通常情况下,一个activity可能包含一个或多个fragment,它们协同工作,组成一个连贯的UI界面。在这种情况下,多个fragments之间的通信显得就很重要了。举个例子,一个activity包含左右两个fragment,左侧的fragment包含了一个列表(比如新闻题目列表),当点击每个新闻题目的时候,右侧的fragment就会显示这条新闻的详尽信息。

下面展示如何进行操作。

Fragment1在整个activity的左侧,Fragment2在右侧。

1.fragment1.xml中的代码。

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#00FF00"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/lblFragment1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="This is fragment #1"  
  13.         android:textColor="#000000"  
  14.         android:textSize="25sp" />  
  15.   
  16. </LinearLayout>  

2.fragment2.xml

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#FFFE00"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="This is fragment #2"  
  12.         android:textColor="#000000"  
  13.         android:textSize="25sp" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/btnGetText"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:onClick="onClick"  
  20.         android:text="Get text in Fragment #1"  
  21.         android:textColor="#000000" />  
  22.   
  23. </LinearLayout>  

3.main.xml中的代码。

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <fragment  
  8.         android:id="@+id/fragment1"  
  9.         android:name="net.learn2develop.Fragments.Fragment1"  
  10.         android:layout_width="0px"  
  11.         android:layout_height="match_parent"  
  12.         android:layout_weight="1" />  
  13.   
  14.     <fragment  
  15.         android:id="@+id/fragment2"  
  16.         android:name="net.learn2develop.Fragments.Fragment2"  
  17.         android:layout_width="0px"  
  18.         android:layout_height="match_parent"  
  19.         android:layout_weight="1" />  
  20.   
  21. </LinearLayout>  

4.FragmentsActivity.java中的代码。

[java] view plain copy
print?
  1. package net.learn2develop.Fragments;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.TextView;  
  7. import android.widget.Toast;  
  8.   
  9. public class FragmentsActivity extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.   
  16.     }  
  17.   
  18.     public void onClick(View v) {  
  19.         TextView lbl = (TextView) findViewById(R.id.lblFragment1);  
  20.         Toast.makeText(this, lbl.getText(), Toast.LENGTH_SHORT).show();  
  21.     }  
  22.   
  23. }  

5.Fragment2.java中的代码。

[java] view plain copy
print?
  1. package net.learn2develop.Fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12. public class Fragment2 extends Fragment {  
  13.     @Override  
  14.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  15.             Bundle savedInstanceState) {  
  16.         // ---Inflate the layout for this fragment---  
  17.         return inflater.inflate(R.layout.fragment2, container, false);  
  18.     }  
  19.   
  20.     @Override  
  21.     public void onStart() {  
  22.         super.onStart();  
  23.         // ---Button view---  
  24.         Button btnGetText = (Button) getActivity()  
  25.                 .findViewById(R.id.btnGetText);  
  26.         btnGetText.setOnClickListener(new View.OnClickListener() {  
  27.             public void onClick(View v) {  
  28.                 // 这里是关键,getActivity()方法返回这个fragment所在的activity实例,通过activity实例可以获取在其中的组件,其他就很简单了。  
  29.                 TextView lbl = (TextView) getActivity().findViewById(  
  30.                         R.id.lblFragment1);  
  31.                 Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT)  
  32.                         .show();  
  33.             }  
  34.         });  
  35.     }  
  36.   
  37. }  


6. 调试。

点击右边的“Get text in Fragment #1”按钮,将弹出一个提示。

0 0
原创粉丝点击