将活动回调发送到片段

问题描述 投票:2回答:3

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

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

我在Activity中创建了一个界面:

    public interface IloadCallBack {
    void onLoadAdapter(List<Suser> userList);
}

我为此创建了一个setter:

    public void setIloadCallBack(IloadCallBack iloadCallBack) {
    this.iloadCallBack = iloadCallBack;
}

并初始化它:

iloadCallBack.onLoadAdapter(susers);

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

android callback
3个回答
3
投票

在我的活动中获取信息时,如何从我的Activity中刷新片段1中的recyclelerView适配器

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

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

// in fragment
public void refreshList(List<Suser> userList){
      this.userList.clear();// empty list
      this.userList.addAll(userList);
      notifyDataSetChanged();
}

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

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();
        ...
    }
}

1
投票

从活动到片段的沟通:

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();
        }
    }
}

从片段到活动的沟通:

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

随意看看那里,他们很好地解释了一切。


0
投票

如果你想在某个其他地方发生任何特定事件时执行某些操作,比如你想在活动中发生事件时执行你的片段中的任何方法,反之亦然,我建议你应该使用EventBus。

https://github.com/greenrobot/EventBus

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

© www.soinside.com 2019 - 2024. All rights reserved.