将活动回调发送到片段

时间:2017-12-25 12:12:39

标签: android callback

我的活动中有一个查看分页器。此寻呼机加载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);

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

3 个答案:

答案 0 :(得分: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 :(得分: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

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

答案 2 :(得分:0)

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

https://github.com/greenrobot/EventBus

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