【问题标题】:Send callback from activity to fragment从活动发送回调到片段
【发布时间】:2017-12-25 21:12:40
【问题描述】:

我的活动中有一个查看寻呼机。此寻呼机加载 2 个片段(Fragment1 和 Fragment2)。 我的活动有一个按钮,用于从服务器获取数据作为我的 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


    【解决方案1】:

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

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

    只需在片段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();
            ...
        }
    }
    

    【讨论】:

      【解决方案2】:

      从 Activity 到 Fragment 的通信:

      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

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

      【讨论】:

      • 老兄请阅读我的问题!如何通过捆绑发送活动?
      • 我阅读了您的问题并发布了答案。通过您的活动更新片段的方法是使用我描述的方法。请再次阅读我的回答。
      【解决方案3】:

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

      https://github.com/greenrobot/EventBus

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

      【讨论】:

      • 我需要参考我的活动。我正在使用 eventBus,但现在我需要在从活动中获取数据后在片段中做一些事情。
      • 是的,你可以做任何你想做的动作,如果你使用事件总线,你的事件总线函数会像特定事件发生一样知道,现在我想执行这段代码,它会工作
      • 如果你想要一个好的事件总线,请查看通过 RxAndroid 实现的事件总线。除此之外,我建议您查看我发布的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多