Android系列之Fragment(三)----Fragment和Activity之间的通信(含接口回调)

简介:

【正文】


一、接口回调的简单引入:

我们先来看一个简单的接口回调的例子:

新建一个Java工程,然后新建一个包。然后新建一个A.java文件:

A.java代码如下:

复制代码
 1 package com.cn.callback;
 2 
 3 public class A {
 4     public A() {
 5         
 6     }
 7     
 8     //下载图片的操作
 9     public void loadImage(String image_path,final CallBack callBack) {
10         new Thread(new Runnable(){
11 
12             public void run() {
13                 // TODO Auto-generated method stub
14                 String msg = "Hello world";
15                 callBack.getResult(msg);                
16             }
17             
18         }).start();
19     }
20     
21     public interface CallBack {
22         public void getResult(String result);
23     }
24 } 
复制代码

第21至23行就是回调方法。

新建B.java,代码如下:

复制代码
 1 package com.cn.callback;
 2 
 3 import com.cn.callback.A.CallBack;
 4 
 5 public class B {
 6     public B(){
 7         
 8     }
 9     
10     public static void main(String args[]) {
11         A a = new A();
12         a.loadImage("http://www.baidu.com/a.gif", new CallBack() {
13             public void getResult(String result) {
14                 // TODO Auto-generated method stub
15                 System.out.println(result);
16             }
17             
18         });
19     }
20 }
复制代码

最后程序运行的结果如下:

关于接口回调,有一个博客,不过现在还不能完全理解,附上链接:

一个经典例子让你彻彻底底理解java回调机制:http://blog.csdn.net/xiaanming/article/details/8703708

 

二、Fragment和Activity的交互:

1、在Fragment中调用Activity中的方法:

Fragment可以通过getActivity()方法来获得Activity的实例,然后就可以调用一些例如findViewById()之类的方法。例如:

View listView = getActivity().findViewById(R.id.list);

但是注意调用getActivity()时,fragment必须和activity关联(attached to an activity),否则将会返回一个null。

另外,当碎片中需要使用Context对象时,也可以使用getActivity()方法,因此获取到的活动本身就是一个Context对象。

【实例】在Activity的EditText中输入一段文本,这个时候,点击Fragment中的按钮,让它弹出吐司,显示出对应的文本。

其实就是让Activity中的文本显示在Fragment中,Fragment的核心代码如下:

复制代码
 1     public View onCreateView(LayoutInflater inflater, ViewGroup container,
 2             Bundle savedInstanceState) {
 3         View view = inflater.inflate(R.layout.fragment_left, null);    
 4         button = (Button) view.findViewById(R.id.button1);
 5         button.setOnClickListener(new OnClickListener() {            
 6             @Override
 7             public void onClick(View v) {
 8                 // TODO Auto-generated method stub
 9                 EditText editText = (EditText) getActivity().findViewById(R.id.editText);
10                 Toast.makeText(getActivity(), editText.getText().toString(), 1).show();
11             }
12         });
13 
14         return view;
15     }
复制代码

第09行代码是核心,通过getActivity()方法来获得Activity的实例,然后就可以调用findViewById()的方法得到其中的EditText控件。

2、在Activity中调用Fragment中的方法:(要用到接口回调

activity也可以获得一个fragment的引用,从而调用fragment中的方法。获得fragment的引用要用FragmentManager,之后可以调用findFragmentById() 或者 findFragmentByTag()。例如:

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

 

具体例子稍后再讲。

3、Fragment与Fragment之间的通信:

既然Fragment和Activity之间的通信问题解决了,那Fragment与Fragment之间的通信也没有那么复杂。基本思路是:

首先在一个Fragment中可以得到与它相关联的Activity,然后再通过这个Activity去获取另外一个Fragment的实例,这样就实现了不同Fragment之间的通信。

三、创建事件回调(在Activity中获取Fragment中的值):

一些情况下,可能需要fragment和activity共享事件,一个比较好的做法是在fragment里面定义一个回调接口,然后要求宿主activity实现这个接口。当activity通过这个接口接收到一个回调,它可以让同布局中的其他fragment分享这个信息。

例如,一个新闻显示应用在一个activity中有两个fragment,一个fragment A显示文章题目的列表,一个fragment B显示文章。所以当一个文章被选择的时候,fragment A必须通知activity,然后activity通知fragment B,让它显示这篇文章。(例子的代码见官方文档)

我们现在举一个其他的例子:

【实例】在Fragment中输入值,点击Activity中的按钮,弹出吐司,显示之前输入的值。其实就是让Fragment中的文本显示在Activity中

我们在平板的左侧加入一个fragment,完整代码如下:

fragment_left.xml代码如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>

</LinearLayout>
复制代码

其实就是加了一个EditText,方便在里面输入文本内容。

然后在frament中加一个接口回调,让它在Activity当中调用,方便获取输入文本的值。LeftFragment.java的代码如下:

复制代码
 1 package com.example.m01_fragment05;
 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.EditText;
10 
11 public class LeftFragment extends Fragment {
12     
13     private Button button;
14     private EditText editText;
15     
16     @Override
17     public void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19     }
20     
21     @Override
22     public View onCreateView(LayoutInflater inflater, ViewGroup container,
23             Bundle savedInstanceState) {
24         View view = inflater.inflate(R.layout.fragment_left, null);
25         editText = (EditText) view.findViewById(R.id.editText1);
26         return view;
27     }
28     
29     @Override
30     public void onPause() {
31         super.onPause();
32     }
33     
34     //接口回调
35     public void getEditText(CallBack callBack) {
36         String msg = editText.getText().toString();
37         callBack.getResult(msg);
38     }
39 
40     public interface CallBack {
41         public void getResult(String result);
42     }
43 }
复制代码

代码解释如下:

第25行:一定要为editText加一个id,不然会报空指针异常的错误;

34至42行:添加一个接口回调,用于获取文本的值,然后稍后再Activity当中进行调用。

activity_main.xml的代码如下:

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

     <LinearLayout
        android:id="@+id/left"
        android:layout_width="224dp"
        android:layout_height="match_parent"
        android:background="#CCCCCC"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="获得Fragment的值" />
    </LinearLayout> 

</LinearLayout>
复制代码

其实一共就两个线性布局,左边的现性布局留给fragment,右边的线性性局留给Activity。

MainActivity.java的代码如下:

复制代码
 1 package com.example.m01_fragment05;
 2 
 3 import com.example.m01_fragment05.LeftFragment.CallBack;
 4 
 5 import android.app.Activity;
 6 import android.app.FragmentManager;
 7 import android.app.FragmentTransaction;
 8 import android.os.Bundle;
 9 import android.view.Menu;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.Button;
13 import android.widget.Toast;
14 
15 public class MainActivity extends Activity {
16     private FragmentManager manager;
17     private FragmentTransaction transaction;
18     private Button button;
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);        
23         button = (Button)findViewById(R.id.button);
24         
25         //动态加载leftFragment
26         manager = getFragmentManager();
27         transaction = manager.beginTransaction();
28         final LeftFragment leftFragment = new LeftFragment();
29         transaction.add(R.id.left, leftFragment, "left");
30         transaction.commit();
31         button.setOnClickListener(new OnClickListener() {
32             
33             @Override
34             public void onClick(View v) {
35                 //点击按钮后,通过接口回调,获取fragment当中EditText的值,并弹出吐司
36                 leftFragment.getEditText(new CallBack(){
37                     @Override
38                     public void getResult(String result) {
39                         // TODO Auto-generated method stub
40                         Toast.makeText(MainActivity.this, result, 1).show();
41                     }                    
42                 });
43             }
44         });
45     }
46 
47     @Override
48     public boolean onCreateOptionsMenu(Menu menu) {
49         // Inflate the menu; this adds items to the action bar if it is present.
50         getMenuInflater().inflate(R.menu.main, menu);
51         return true;
52     }    
53 }
复制代码

我们在Activity当中动态加载Fragment,然后点击按钮,通过接口回调,获取fragment当中EditText的值,并弹出吐司。

程序运行后,在左侧的Fragment的EditText当中输入值,点击右侧的按钮,弹出吐司,效果如下:

 

相关文章
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
42 1
|
3月前
|
数据库 Android开发 开发者
Android基础知识:请解释Activity的生命周期。
Android基础知识:请解释Activity的生命周期。
43 2
|
3月前
|
Linux Android开发
Android 正常运行所需的一系列 Linux 内核接口
Android 正常运行所需的一系列 Linux 内核接口
53 0
|
1天前
|
Android开发
Android JNI与CAN通信遇到的问题总结
Android JNI与CAN通信遇到的问题总结
14 1
|
1天前
|
存储 Java Linux
Android系统获取event事件回调等几种实现和原理分析
Android系统获取event事件回调等几种实现和原理分析
11 0
|
2月前
|
Android开发
[Android 四大组件] --- Activity
[Android 四大组件] --- Activity
22 1
|
3月前
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
286 54