如何更新从母体内的活性的研究数据片段 [英] How to update data inside fragment from parent acitivity

查看:191
本文介绍了如何更新从母体内的活性的研究数据片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设计是这样的:

-----------------------------------
|   search input | spinner        |
-----------------------------------
| item 1                          |
| item 2                          |
| item 3                          |
-----------------------------------

A 片段(底部)持有的从父活动传下来的记录列表。数据通过捆绑传递是这样创建片段时:

A fragment (bottom part) holds the list of records passed down from the parent activity. The data is passed through bundle when fragment is created like this:

fragmentTagsList = new FragmentTagsSelector();
Bundle fragmentArgs = new Bundle();
fragmentArgs.putParcelableArrayList("tags", tags);
fragmentTagsList.setArguments(fragmentArgs);

if (getFragmentManager().findFragmentById(R.id.tagsListFragmentWrapper) == null) {
    getFragmentManager().beginTransaction().add(R.id.tagsListFragmentWrapper, fragmentTagsList).commit();
}

顶控制(搜索输入微调)属于父活动的UI。每当使用者输入查询到搜索输入,我需要过滤列表,并通过项目的片段的新过滤列表。我该怎么办呢?我应该叫从活性片段的方法是什么?这是一般的分离控制有一个好方法?

The top controls (search input and spinner) belong to parent activity's UI. Whenever a user types a query into search input, I need to filter the list and pass new filtered list of items to the fragment. How can I do that? Should I call the fragment's method from activity? Is this a good approach to separate there controls in general?

推荐答案

您可以让一个接口,并有活动实现它。该重写功能将是其返回的实时数据(HashMap中或数组形式根据你的设计)的人。

You can make an interface and have the Activity implement it. The overriden functions would be the ones which return real time data (in form of HashMap or array as per your design).

 @Override
 public DataType getMyData()
    {
       ....
        return latestFilteredData;
    }

在片段onAttach回调,初始化界面的对象。

In fragment onAttach callback, initialize interface's object.

 @Override
public void onAttach(Activity activity){
    callback = (MyInterface) activity;

    super.onAttach(activity);
}

现在,您可以使用回调对象在样活性的任何地方获得最新数据:

Now you can use callback object to get latest data anywhere in activity like:

 callback.getMyData();

这篇关于如何更新从母体内的活性的研究数据片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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