【问题标题】:Sending data from activity to fragment with interface listener使用接口侦听器将数据从活动发送到片段
【发布时间】:2017-10-05 00:48:10
【问题描述】:

我正在尝试将数据从活动发送到片段。 我没有将数据从片段发送到活动。除了在活动中实例化接口侦听器对象之外,我已经正确设置了所有内容。

public class Activity extends AppCompatActivity {

  private FragmentInterface fragmentInterfaceListener;

  @Override 
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This line below is actually in a button onClick()
    fragmentInterfaceListener.sendDataMethod(dataToSend);

  }

  public interface FragmentInterface {
    void sendDataMethod(SampleData sampleData);
    }
  }

然后在片段中,我有:

public static class CustomFragment extends Fragment implements Activity.FragmentInterface {

  @Override
  public void sendDataMethod(final SampleData sampleData) {

  }    
}

当我在按钮onClick() 中放入日志行时,单击按钮时会出现日志行。不,我不会将 sampleData 放入片段包中。是的,我需要通过接口发送数据。那么如何正确实例化Activity中的fragmentInterfaceListener对象呢?我在 Activity 或 CustomFragment 中是否遗漏了任何其他内容?

【问题讨论】:

  • 你要问清楚。不要偷懒放代码
  • 与其在活动中保留FragmentInterface 接口,不如在活动中保留CustomFragment 片段。然后您可以在CustomFragment 片段中声明公共方法,以便您的活动可以轻松使用这些方法。

标签: android android-fragments


【解决方案1】:

这里缺少的是注册部分。

片段必须向活动侦听器注册自身,以便活动在事件发生时发送数据。为此,请在活动中创建一个方法

private void setOnDataListener(FragmentInterface interface){
    fragmentInterfaceListener=interface;
}

并且在你的片段的 oncreate 中设置这样的监听器

((YOUR_ACTIVITY_NAME)getActivity()).setOnDataListener(this);

【讨论】:

  • 我做了同样的方法,但是在我的活动类中去“NullPointerException:尝试调用接口方法'void com.example.appname.Interfaces”
【解决方案2】:

您不需要在 Fragment 中使用监听器,因为您可以直接从其 宿主 Activity 与 Fragment 进行通信。

正如@lq-gioan 所说,您可以在 Fragment 中创建一个公共方法,然后从您的活动中调用它。因此,创建一个公共方法来设置数据,如下所示:

public static class CustomFragment extends Fragment {

  // public method to be accessed by host activity.
  public void sendDataMethod(final SampleData sampleData) {

  }    
}

然后你可以在你的宿主活动中调用该方法:

CustomFragment fragment = (CustomFragment)getSupportFragmentManager()
                           .findFragmentById(R.id.fragment_id);

// or use find by tag if you adding the fragment by tag
// CustomFragment fragment = (CustomFragment)getSupportFragmentManager()
//                           .findFragmentByTag("FragmentTag");

// now you can call it
fragment.sendDataMethod(yourSampleData);

【讨论】:

  • 我可以在宿主活动中保存对我的片段的引用,然后调用它的方法吗?不是每次都findFragmentById。
  • 当然,您可以将所有片段引用保存到一个列表中,然后使用它来调用该方法。但是你需要确保你没有调用空片段。
  • 谢谢!也许你遇到过和这个人一样的问题? stackoverflow.com/questions/20355127/… 如果是 - 你是如何解决的,因为接受的答案看起来无效。
  • 我正在使用来自这个guides.codepath.com/android/viewpager-with-fragmentpageradapterSmartFragmentStatePagerAdapter。解决view pager的fragment管理问题。
【解决方案3】:

对于从 Activity 向 Fragment 发送数据,我们不需要接口。

可以直接调用Fragment中的方法,也可以在Bundle中作为setArguments传递

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

您可以参考https://developer.android.com/training/basics/fragments/communicating.html

【讨论】:

    猜你喜欢
    • 2020-03-31
    • 2018-09-02
    • 2013-07-12
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2017-07-25
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多