简体   繁体   English

适配器在Mvp模式中的作用?

[英]The role of adapters in Mvp pattern?

How do you treat adapters in MVP pattern? 你如何对待MVP模式中的适配器? For example, in this project https://github.com/msahakyan/nested-recycler-view there is a MovieAdapter, https://github.com/msahakyan/nested-recycler-view/blob/master/app/src/main/java/com/android/msahakyan/nestedrecycler/adapter/MovieAdapter.java this guy has an recyclierview as item in this adapter , (if you look at his project, he has nested recyclierview in his home screen.) Because he has such an item, he doing service call and other notify, loading data(fetching from service) etc. operation in this adapter.(No communicaation to related fragment/activity) As you can see, this adapter has lots of to do. 例如,在这个项目https://github.com/msahakyan/nested-recycler-view中有一个MovieAdapter, https://github.com/msahakyan/nested-recycler-view/blob/master/app/src/ main / java / com / android / msahakyan / nestedrecycler / adapter / MovieAdapter.java这个家伙有一个recyclierview作为这个适配器中的项目,(如果你看看他的项目,他已经在他的主屏幕上嵌套了recyclierview。)因为他有这样的一个项目,他进行服务调用和其他通知,加载数据(从服务中获取)等操作在这个适配器中。(没有通信到相关的片段/活动)正如你所看到的,这个适配器有很多工作要做。 If you would do that, how would you implement this in mvp pattern? 如果你这样做,你将如何在mvp模式中实现这一点? Specifically, in this context, would you have presenter object in adapter, or have view object to do that calling,notifying and loading? 具体来说,在这种情况下,你会在适配器中有presenter对象,还是有视图对象来进行调用,通知和加载?

Adapter is pure View layer. 适配器是纯视图层。 Map all interaction like a click on a button in a RecyclerView ViewHolder back to the Activity / Fragment and then let forward that to the Activity's / Fragment's presenter. 映射所有交互,例如单击RecyclerView ViewHolder中的按钮返回到Activity / Fragment,然后将其转发到Activity的/ Fragment的演示者。

RecyclerView are a little bit trickier because the ViewHolders can be recycled during scrolling etc. By going the road back to the "parent" Activity / Fragment and the corresponding Presenter it is much easier and less error prone to update a ViewHolder (ie with animations by using DiffUtils ). RecyclerView有点棘手,因为ViewHolders可以在滚动等过程中回收。通过回到“父”Activity / Fragment和相应的Presenter,更容易且更不容易更新ViewHolder(即动画由使用DiffUtils)。 Take a ViewHolder just as a way to display a data object but don't add a Presenter for each ViewHolder to coordinate the ViewHolder. 将ViewHolder作为显示数据对象的方式,但不要为每个ViewHolder添加Presenter来协调ViewHolder。 Really, just ensure that the ViewHolder gets a data object containing all information the ViewHolder needs to display but don't make this data object be "controlled" by a ViewHolders Presenter. 实际上,只需确保ViewHolder获取包含ViewHolder需要显示的所有信息的数据对象,但不要使ViewHolders Presenter“控制”此数据对象。 Otherwise, you end up with a mess because one ViewHolder gets update by his Presenter , maybe the ViewHolder has been recycled in the mean time, maybe a screen orientation change has occurred or maybe the "parent" Activity / Fragment Presenter has updated the adapter's whole dataset etc. Do yourself a favor and use only one Presenter "coordinating / controlling" the data ther RecyclerView should display by using "parents" Activity / Fragment Presenter. 否则,你最终会弄得一团糟,因为一个ViewHolder得到了他的Presenter的更新,也许ViewHolder在同一时间被回收,可能是屏幕方向发生了变化,或者“父”Activity / Fragment Presenter更新了适配器的整体帮自己一个忙,只使用一个Presenter“协调/控制”RecyclerView应该使用“父母”Activity / Fragment Presenter显示的数据。

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

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