简体   繁体   English

通过Android MVP中的构造函数将片段(View)Presenter传递给适配器

[英]Passing Fragment's (View) Presenter to Adapter Through Constructor in Android MVP

I have a MVP Fragment (View) that hosts a custom implementation of a RecyclerView adapter (RecyclerViewAdapter). 我有一个MVP片段(视图),它承载RecyclerView适配器(RecyclerViewAdapter)的自定义实现。 This adapter extends my custom RecyclerViewAdapterBase adapter. 此适配器扩展了我的自定义RecyclerViewAdapterBase适配器。 As the Presenter hosting the Fragment needs to know what data is currently modelled, the data which the RecyclerView will utilize is stored as a private field in the Presenter. 由于托管Fragment的Presenter需要知道当前建模的数据,RecyclerView将使用的数据将作为私有字段存储在Presenter中。

This being said, I am currently passing the Presenter to the RecyclerViewAdapter through the Adapter's constructor. 话虽如此,我目前正通过Adapter的构造函数将Presenter传递给RecyclerViewAdapter。 Is this a risk? 这有风险吗?

The only risk I can see is the RecyclerViewAdapter outliving the Fragment and thus outliving the Presenter. 我能看到的唯一风险是RecyclerViewAdapter比片段寿命更长,因此比Presenter更长久。 However, this is not a possibility as the Fragment holds a private reference to the Adapter and the Presenter and both would be destroyed at the same time. 但是,这不可能,因为Fragment拥有对Adapter和Presenter的私有引用,并且两者都会同时被销毁。

This being said, I've thought of only passing the data required to the RecyclerViewAdapter in the constructor, but then I am not certain how to update the data without a reference to the Presenter? 话虽如此,我想过只在构造函数中传递RecyclerViewAdapter所需的数据,但是我不确定如何在不引用Presenter的情况下更新数据? (Where the data is currently modelled.) (当前数据的建模位置。)

Thank you for your ideas! 谢谢你的想法!

Presenter hosting the Fragment needs to know what data is currently modelled 托管Fragment的Presenter需要知道当前建模的数据

You just pass the data that you want to present, don't store it inside a presenter. 您只需传递要显示的数据,不要将其存储在演示者中。

I am currently passing the Presenter to the RecyclerViewAdapter through the Adapter's constructor. 我目前正通过Adapter的构造函数将Presenter传递给RecyclerViewAdapter。 Is this a risk? 这有风险吗?

No. Adapter is just an object, it does not have its own lifecycle like Activity or Thread does. 不。适配器只是一个对象,它没有像ActivityThread那样的生命周期。 If activity gets destroyed -> adapter gets destroyed. 如果活动被破坏 - >适配器被破坏。 If adapter gets destroyed -> presenter gets destroyed. 如果适配器被破坏 - >演示者被破坏。 No leaks here, you are good. 这里没有泄漏,你很好。

This being said, I've thought of only passing the data required to the RecyclerViewAdapter in the constructor, but then I am not certain how to update the data without a reference to the Presenter? 话虽如此,我想过只在构造函数中传递RecyclerViewAdapter所需的数据,但是我不确定如何在不引用Presenter的情况下更新数据?

Depends on how frequently you are going to use your presenter. 取决于您使用演示者的频率。 If you only need to draw it once, then you can create a presenter, use it once and thus avoid the reference to the presenter. 如果您只需要绘制一次,那么您可以创建一个演示者,使用它一次,从而避免引用演示者。 But, you have an adapter which represents many views, so, I'm pretty sure you are going to draw a lot. 但是,你有一个代表许多视图的适配器,所以,我很确定你会画很多东西。 So keep a reference to the presenter from your adapter, this is absolutely fine. 所以从你的适配器保持对演示者的引用,这绝对没问题。 There are no memory and performance concerns here. 这里没有内存和性能问题。

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

相关问题 适配器作为演示者? 或者与演示者交谈? Android和MVP - Adapter as Presenter? Or talking with a Presenter? Android and MVP 将演示者传递给Android中的Fragment(没有构造函数) - Passing a presenter to a Fragment in Android (without a constructor) Android MVP-将字符串资源从演示者传递到视图 - Android MVP - passing string resources from the presenter to the view Android MVP - 从活动展示器到片段展示器的数据 - Android MVP - Data from activity presenter to fragment presenter Android MVP - 如何在活动演示者和片段演示者之间进行通信 - Android MVP - How to communicate between activity presenter and fragment presenter Android MVP-活动应该是视图还是演示者? - Android MVP - should an Activity be a View or Presenter? Android MVP-查看单元测试-设置演示者 - Android MVP - View unit tests - setting the presenter Android MVP - 在无头片段中保留演示者时如何确保在配置更改期间视图获得更新? - Android MVP - when retaining presenter in headless fragment how to ensure view gets update during config change? 在适配器的构造函数中传递Fragment - Passing a Fragment in the constructor of an Adapter android MVP - 具有多个模型的Presenter - android MVP - Presenter with multiple model's
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM