【问题标题】:Android : best case for communicating adapter with Activity/FragmentAndroid:与 Activity/Fragment 通信适配器的最佳情况
【发布时间】:2017-05-07 22:00:12
【问题描述】:

在 MVP 模式中,用户的动作必须传递给演示者

所以当用户单击项目时在 listView 中我想将此操作发送给演示者

适配器与 Activity/Fragment 通信的最佳情况是什么?

如果你能解释每一个的优缺点,我将不胜感激。

  1. 事件总线
  2. 回调

【问题讨论】:

标签: android android-adapter mvp event-bus


【解决方案1】:

ViewPresenter 之间的通信应该通过interface

PresenterActivity(视图)都有自己的interface

  1. 因此,列表项单击项被转发给演示者。
  2. 演示者决定做什么。即打开一个 DetailsAccticity
  3. 调用 Activity 以启动新 Intent。

这是 MVP interfaces 的一个很好的例子

MVP的良好/简要解释

编辑

  • Adapter & Activity/Fragment 之间的通信。

第一点——在我个人看来AdapterActivity/Fragment都是MVP架构中View的子部分。

Activity 将引用Adapter

Activity(通过Presenter)可以通过通常的方式来维护适配器中的数据,即更改底层数据对象并调用notifyDataSetChanged

至于Adapter 将请求发送回ActivityView.OnClickListener 这可以通过Adapter 首次初始化时发送的回调来完成(即使用new 关键字创建)

【讨论】:

  • tnx 响应,但我的问题是关于适配器与片段之间的通信,而不是演示者/视图
【解决方案2】:

根据您的情况,您可以采用以下方法

Try using an interface

https://stackoverflow.com/a/16443645/4247543

if the above method is not helping then try using EventBus

请点击以下链接了解更多事件总线

https://github.com/greenrobot/EventBus

http://gunhansancar.com/ease-communication-between-activities-fragments-services/

BroadcastReceiver as mentioned could also help for enabling communication.

https://stackoverflow.com/a/10084754/4247543

You can use observers

https://stackoverflow.com/a/30964385/4247543

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    您始终可以使用回调来监听点击。您应该实现的是使用接口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多