在具有两个(或多个)片段的单个活动上实施MVP [英] Implementing MVP on a single activity with two (or multiple) fragments

查看:82
本文介绍了在具有两个(或多个)片段的单个活动上实施MVP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个显示列表的小型应用程序,单击某个项目后,它将打开一个包含该项目详细信息的辅助屏幕。我想将MVP用作此应用程序的体系结构,并且当我包含两个片段的单个活动时,我一直在努力寻找解决方法。

I'm developing a small application that shows a list, and when an item is clicked it opens a secondary screen with the item details. I want to implement MVP as my architecture for this app, and i have been struggling figuring out how to do that when I have a single activity with 2 fragments.

出现一些问题,例如,单击列表中的某个项目时,将向第一个演示者发送一个回调,但是此时,谁负责打开第二个片段?主持人之间是否互相交谈?我应该通过活动来做到这一点吗?

Some questions came up as when an item from the list is clicked, a callback is sent to the first presenter, but at this point, who is in charge of opening the second fragment? do the presenters 'talk' to each other? should i do it through the activity?

一直在寻找单个活动的示例,其中包含实现MVP的多个片段,但是找不到类似的东西。

Been looking around for examples of single activity with multiple fragments implementing MVP, but couldn't find something like that yet.

(是的,可以这样做,但是应用程序的目的是学习在具有多个片段的单个活动上实现MVP)

(Yes, it can be done otherwise, but the purpose of the app is to learn implementing MVP on a single activity with multiple fragments)

感谢任何帮助!
谢谢!

Appreciate any help! Thanks!

推荐答案

在研究了不同的现有MVP示例项目之后,我决定遵循Google的 TODO-MVP-TABLET git项目,可在此处找到:

After looking into different existing MVP sample projects I've decided to follow most of the concepts in the 'TODO-MVP-TABLET' git project by Google which can be found here:

https://github.com/googlesamples/android-architecture/tree/dev-todo-mvp-tablet

我之所以选择此解决方案,是因为其抽象水平以及以后可以在不更改任何代码的情况下在其他活动中重用任何片段的能力。

I've chosen this solution due to the level of abstraction and the ability to later on reuse any fragment in other activities without changing any code.

解决方案原理:


  • 每个片段都有一个由接口定义的演示者。

  • 有一个较大的演示者,它实现了所有较小的演示者。

  • 较大的演示者持有对所有较小的演示者的引用,并且在调用方法时,它只是委托有关的行动

  • 片段被赋予大演示者作为其演示者,而实际上并没有意识到这种情况。

  • 较小的演示者应持有引用

  • Each fragment has a presenter defined by an interface.
  • There is a bigger presenter implementing all the smaller presenters.
  • The bigger presenter holds references to all of the smaller presenters and when a method is invoked it simply delegates the action to the relevant presenter.
  • Fragments are given the 'big' presenter as their presenter without actually being aware this is the case.
  • Smaller presenters should hold the reference to the view.

从Google的github页面获取的图表:

更新
链接无效,好像Google从他们的样品。如果他们重新上传,则会保留它。

Update: Link isn't valid, seems like Google removed the project from their samples. Will leave it in case they reupload it.

这篇关于在具有两个(或多个)片段的单个活动上实施MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆