`
king_tt
  • 浏览: 2114047 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【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中的代码。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <TextView
  8. android:id="@+id/lblFragment1"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="Thisisfragment#1"
  12. android:textColor="#000000"
  13. android:textSize="25sp"/>
  14. </LinearLayout>
2. fragment2.xml
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Thisisfragment#2"
  11. android:textColor="#000000"
  12. android:textSize="25sp"/>
  13. <Button
  14. android:id="@+id/btnGetText"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:onClick="onClick"
  18. android:text="GettextinFragment#1"
  19. android:textColor="#000000"/>
  20. </LinearLayout>
3. main.xml中的代码。
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <fragment
  7. android:id="@+id/fragment1"
  8. android:name="net.learn2develop.Fragments.Fragment1"
  9. android:layout_width="0px"
  10. android:layout_height="match_parent"
  11. android:layout_weight="1"/>
  12. <fragment
  13. android:id="@+id/fragment2"
  14. android:name="net.learn2develop.Fragments.Fragment2"
  15. android:layout_width="0px"
  16. android:layout_height="match_parent"
  17. android:layout_weight="1"/>
  18. </LinearLayout>
4. FragmentsActivity.java中的代码。
  1. publicclassFragmentsActivityextendsActivity{
  2. /**Calledwhentheactivityisfirstcreated.*/
  3. @Override
  4. publicvoidonCreate(BundlesavedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. }
  8. publicvoidonClick(Viewv){
  9. TextViewlbl=(TextView)findViewById(R.id.lblFragment1);
  10. Toast.makeText(this,lbl.getText(),Toast.LENGTH_SHORT).show();
  11. }
  12. }
5. Fragment2.java中的代码。
  1. publicclassFragment2extendsFragment{
  2. @Override
  3. publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
  4. BundlesavedInstanceState){
  5. //---Inflatethelayoutforthisfragment---
  6. returninflater.inflate(R.layout.fragment2,container,false);
  7. }
  8. @Override
  9. publicvoidonStart(){
  10. super.onStart();
  11. //---Buttonview---
  12. ButtonbtnGetText=(Button)getActivity()
  13. .findViewById(R.id.btnGetText);
  14. btnGetText.setOnClickListener(newView.OnClickListener(){
  15. publicvoidonClick(Viewv){
  16. TextViewlbl=(TextView)getActivity().findViewById(
  17. R.id.lblFragment1);
  18. Toast.makeText(getActivity(),lbl.getText(),Toast.LENGTH_SHORT)
  19. .show();
  20. }
  21. });
  22. }
  23. }
6. 调试。

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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics