用于将数据从片段/活动片段/活性接口名称技术 [英] Name technique for passing data from fragment/activity to fragment/activity with interfaces

查看:162
本文介绍了用于将数据从片段/活动片段/活性接口名称技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学校里,我们现在学习如何使用接口片段更加普遍。 这种技术仍然有点抽象,我真的不知道什么时候/如何使用它。

At school we're now learning on how to make fragments more universal by using interfaces. This technique is still kinda abstract and I don't really know when/how to use it.

任何人都可以点我的一些资源如何使用这种技术(难道是所谓的接口回调?)

Can anybody point me to some resources on how to use that technique (Could it be called interface callbacks?)

所有帮助是非常AP preciated!

All help is very appreciated!

推荐答案

回调方法,因为你会称呼它,很简单,只要监听器界面中使用Java或者Android的许多地方。您可以查看 Observer模式 ,如果​​你想了解一个非常一般描述。但是,如果你已经知道如何使用监听器的工作,你很容易就会对焦点回调

The callback approach, as you would call it, is as simple as Listener interface found in many parts of Java or Android. You may check the Observer pattern if you want to learn about a very general description. But if you already understand how to work with Listener, you will easily get the point about callbacks.

注意:不要用 回调长期混吧 - 这些是不一样的

NOTE: Do not mix it with Callback term - these are not the same.

假设我们有活动 MyActivity 片段 MyFragment。我们想发布一些数据片段的活动。那就让我们在创建一个接口 MyFragment

Suppose we have Activity MyActivity and Fragment MyFragment. We want to post some data from Fragment to Activity. Then let us create an interface within MyFragment:

public class MyFragment extends Fragment{

    private PostDataCallback mCallback;//our Activity will implement this

    @Override
    public void onAttach(Activity activity) {
         super.onAttach(activity);

         // This makes sure that the container activity has implemented
         // the callback interface. If not, it throws an exception
         try {
             mCallback = (PostDataCallback) activity;
         } catch (ClassCastException e) {
             throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
         }
     }


    public interface PostDataCallback{
        public void onPostData(Object data);
    }

   /*
    we trigger this method when we calculated
         data or something like that and want to post it*/

    public void onSomeEvent(Object data){

        mCallback.onPostData(data);
    }
}

我们的 MyActivity 会是这样的:

public class MyActivity extends Activity implements MyFragment.PostDataCallback{

    private Object data;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        getFragmentManager().beginTransaction().add(R.id.some_container_id, new MyFragment(), "my fragment");


    }

    @Override
    public void onPostData(Object data){
        this.data = data;
        //some operations

    }

}    

因此​​, MyFragment 一无所知执行它的回调。但它知道,它可以调用的方法 onPostData(对象o) PostDataCallback ,这是在变量中<$ C实例$ C> mCallback 。

So, MyFragment knows nothing about the implementation of it's callback. But it knows, that it can call the method onPostData(Object o) on the instance of PostDataCallback, which is held in the variable mCallback.

因此​​,当 MyFragment 触发它的 mCallback.onPostData(数据) MyActivity 取得的结果。

Thus, when MyFragment triggers it's mCallback.onPostData(data), MyActivity get's the result.

完全一样的方法是有效的,如果我们想从 MyActivity 发送消息给 MyFragment ,但我们会做到这一点做到这一点,反之亦然:触发方式,回调接口定义和实例将驻留在 MyActivity MyFragment 将实施的接口。

Exactly the same approach would work if we wanted to send message from MyActivity to MyFragment, but we would do it do it vice versa: the trigger method, callback interface definition and instance would reside in MyActivity, and MyFragment would implement the interface.

这篇关于用于将数据从片段/活动片段/活性接口名称技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆