Android Activity+Fragment及它们之间的数据交换

简介:

为什么要用Fragment?使用Fragment可以在一个Activity中实现不同的界面。Fragment与Fragment之间的动画切换,远比Activity与Activity之间的动画切换变化方式多。很多时候,我们通过使用一个Activity,切换多个Fragment。本次博客,主要列举一下Fragment与它的Activity之间进行数据交换的方式。

1.Fragment中通过getActivity()然后进行强制转化,调用Activity中的公有方法

((XXXXActivity)getActivity()).fun();

2.Activity在切换Fragment的时候,通过setArguments向Fragment传递参数,Fragment通过getArguments();获得从activity中传递过来的值

3.Activity实现一个接口,Fragment在onAttach方法中,将该Activity转化为该接口,在需要调用的时候回调。

注意:本Demo是通过FragmentManager来管理Fragment的,通过FragmentManager管理,我们创建Fragment和销毁Fragment的时候,可以通过栈的方式:

a.FragmentTransaction的add方法,添加一个Fragment

b.FragmentTransaction的popBackStack()弹出该Fragment

演示实例:

fragment1.xml

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#FFFFFFFF"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.testfragment.MainActivity$PlaceholderFragment" >  
  11.     <TextView  
  12.         android:id="@+id/tv"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:text="fragment1" />  
  17.     <Button  
  18.         android:id="@+id/btn"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_below="@id/tv"  
  22.         android:text="跳转到Fragment2" />  
  23. </RelativeLayout>  

MyFragment1.java

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. /* 
  2.  * $filename: MyFragment.java,v $ 
  3.  * $Date: 2014-5-16  $ 
  4.  * Copyright (C) ZhengHaibo, Inc. All rights reserved. 
  5.  * This software is Made by Zhenghaibo. 
  6.  */  
  7. package com.example.testfragment;  
  8.   
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.support.v4.app.Fragment;  
  12. import android.view.LayoutInflater;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.view.ViewGroup;  
  16. import android.widget.Button;  
  17.   
  18. /* 
  19.  *@author: ZhengHaibo   
  20.  *web:     http://blog.csdn.net/nuptboyzhb 
  21.  *mail:    [email protected] 
  22.  *2014-5-16  Nanjing,njupt,China 
  23.  */  
  24. public class MyFragment1 extends Fragment {  
  25.       
  26.     FragmentCallBack fragmentCallBack = null;  
  27.     Button btn;  
  28.     @Override  
  29.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  30.             Bundle savedInstanceState) {  
  31.         View rootView = inflater.inflate(R.layout.fragment1, container,  
  32.                 false);  
  33.         btn = (Button)rootView.findViewById(R.id.btn);  
  34.         btn.setOnClickListener(new OnClickListener() {  
  35.               
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 // TODO Auto-generated method stub  
  39.                 fragmentCallBack.callbackFun1(null);  
  40.             }  
  41.         });  
  42.         return rootView;  
  43.     }  
  44.       
  45.     @Override  
  46.     public void onAttach(Activity activity) {  
  47.         // TODO Auto-generated method stub  
  48.         super.onAttach(activity);  
  49.         fragmentCallBack = (MainActivity)activity;  
  50.     }  
  51. }  

fragment2.xml

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#FFFFFFFF"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.testfragment.MainActivity$PlaceholderFragment" >  
  11.     <TextView  
  12.         android:id="@+id/tv"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:text="fragment2" />  
  17.     <Button  
  18.         android:id="@+id/btn1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_below="@id/tv"  
  22.         android:text="直接调用Activity" />  
  23.     <Button  
  24.         android:id="@+id/btn2"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_below="@id/btn1"  
  28.    &nbsp

    再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/skiwnchhw/p/10349623.html