首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将回调从活动发送到片段

将回调从活动发送到片段
EN

Stack Overflow用户
提问于 2017-12-25 12:12:39
回答 3查看 5.4K关注 0票数 2

我的活动中有一个视图寻呼机。这个寻呼机装载两个片段(Fragment1和Fragment2)。我的活动有一个按钮从服务器获取数据,作为我的pojo类的列表。Fragment1和Fragment2包含recyclerView。

我的问题是,当在我的活动中获取信息时,如何刷新Fragment1的recyclerView适配器(来自我的活动)?

我在我的活动中创建了一个接口:

代码语言:javascript
复制
    public interface IloadCallBack {
    void onLoadAdapter(List<Suser> userList);
}

我为此创造了一个策划人:

代码语言:javascript
复制
    public void setIloadCallBack(IloadCallBack iloadCallBack) {
    this.iloadCallBack = iloadCallBack;
}

在此基础上:

代码语言:javascript
复制
iloadCallBack.onLoadAdapter(susers);

现在,我已经在我的片段中引用了一些活动,但我认为这是错误的!是?我能做什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-12-25 12:32:53

当在活动中获取信息时,如何从活动中刷新片段1中的recyclerView适配器?

您不需要回调机制将数据传递给活动中托管的片段。

只需在片段refreshList中创建一个方法

代码语言:javascript
复制
// in fragment
public void refreshList(List<Suser> userList){
      this.userList.clear();// empty list
      this.userList.addAll(userList);
      notifyDataSetChanged();
}

保持对片段实例的全局引用,并从接收响应的位置调用refreshList

代码语言:javascript
复制
public class YourActivity...{
    private Fragment fragmentInstance;


    void someMethodReceivedNewList(){
        // where you receive new list in activity
        if(fragmentinstance!=null)
            fragmentinstance.refreshList(userList);

    }

    void someMethodToLoadFragment(){
        fragmentInstance = new YourFragment1();
        ...
    }
}
票数 3
EN

Stack Overflow用户

发布于 2017-12-25 12:29:35

从活动到片段的交流:

代码语言:javascript
复制
public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article

        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);
        } else {
            // Otherwise, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
}

从片段到活动的交流:

代码语言:javascript
复制
public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @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 = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    ...
}

这两个都是从https://developer.android.com/training/basics/fragments/communicating.html取来的

请随便看看那里,他们解释得很好。

票数 2
EN

Stack Overflow用户

发布于 2017-12-25 12:26:14

如果您希望在其他地方发生任何特定事件时执行某些操作,比如在活动中发生事件时希望在片段中执行任何方法,或者相反,我将建议您使用EventBus。

https://github.com/greenrobot/EventBus

这是简单而直接的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47968500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档