简体   繁体   English

适配器作为演示者? 或者与演示者交谈? Android和MVP

[英]Adapter as Presenter? Or talking with a Presenter? Android and MVP

I'm trying to follow the MVP pattern. 我正在尝试遵循MVP模式。 However, I have some doubts on how to handle adapters and view holders on this pattern. 但是,我对如何在这种模式下处理适配器和视图持有者有一些疑问。

Should I use the adapter as a presenter? 我应该使用适配器作为演示者吗? Having the business logic? 有业务逻辑吗?

Or should I pass a instance of the presenter that handles the list logic and then call methods of the presenter when there is any interaction with the adapter elements? 或者我应该传递处理列表逻辑的演示者实例,然后在与适配器元素进行任何交互时调用演示者的方法?

Thank you 谢谢

There is no exact/correct definition of implementing MVP in Android 在Android中实施MVP没有确切/正确的定义

To answer your question, in my view the Presenter should not have any Android logic. 要回答您的问题,在我看来, Presenter不应该有任何Android逻辑。

As such, the Adapter would be a " View " then ie Presenter provides it the data (via the Activity or Fragment ) and it just deals with how to present this. 因此, Adapter将是“ View ”然后即,Presenter为其提供数据(通过ActivityFragment ),它只是处理如何呈现它。

I'd do MVP as follow. 我按照以下方式做MVP。

  1. Model - POJO's, parsing, Storing (SQLlite) and retrieving data (http). 模型 - POJO,解析,存储(SQLlite)和检索数据(http)。 Obviously I'd divide the POJO's, parsing and DB logic into sub folders - but this all falls into Model for me. 显然我将POJO,解析和数据库逻辑划分为子文件夹 - 但这一切都属于我的模型。

  2. View - Activity , Fragment , Adapters - Activities & Fragment hold reference to a Presenter that gives them data to display. 视图 - ActivityFragmentAdapters - 活动和片段保持对演示者的引用,该演示者为其提供要显示的数据。 How this data/messages are displayed, look + feel etc. is dealt with in the View . 如何显示这些数据/消息,外观+感觉等在View处理。

  3. Presenter - The Middle man, provides the logic to inputs ie Button Clicks, retrieval of data, validation of inputs & then passes the result back to the View ( Activity or Fragment ) 演示者 - 中间人,为输入提供逻辑,即按钮点击,数据检索,输入验证,然后将结果传递回视图( ActivityFragment

Here's a great article on MVP 这是一篇关于MVP的精彩文章

Here's a simplified diagram of MVP 这是MVP的简化

在此输入图像描述

Answer modified from this question (also answered by me) 从这个问题修改的答案(也由我回答)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM