【Android 开发教程】Fragments间的交互

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


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

下面展示如何进行操作。

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

1. fragment1.xml中的代码。

[java] view plain copy
  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
[java] view plain copy
  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中的代码。
[java] view plain copy
  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
  1. public class FragmentsActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.   
  8.     }  
  9.   
  10.     public void onClick(View v) {  
  11.         TextView lbl = (TextView) findViewById(R.id.lblFragment1);  
  12.         Toast.makeText(this, lbl.getText(), Toast.LENGTH_SHORT).show();  
  13.     }  
  14.   
  15. }  
5. Fragment2.java中的代码。
[java] view plain copy
  1. public class Fragment2 extends Fragment {  
  2.     @Override  
  3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  4.             Bundle savedInstanceState) {  
  5.         // ---Inflate the layout for this fragment---  
  6.         return inflater.inflate(R.layout.fragment2, container, false);  
  7.     }  
  8.   
  9.     @Override  
  10.     public void onStart() {  
  11.         super.onStart();  
  12.         // ---Button view---  
  13.         Button btnGetText = (Button) getActivity()  
  14.                 .findViewById(R.id.btnGetText);  
  15.         btnGetText.setOnClickListener(new View.OnClickListener() {  
  16.             public void onClick(View v) {  
  17.                 TextView lbl = (TextView) getActivity().findViewById(  
  18.                         R.id.lblFragment1);  
  19.                 Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT)  
  20.                         .show();  
  21.             }  
  22.         });  
  23.     }  
  24.   
  25. }  
6. 调试。

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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值