在 Android MVP 中通过构造函数将 Fragment 的(View)Presenter 传递给 Adapter
Passing Fragment's (View) Presenter to Adapter Through Constructor in Android MVP
我有一个 MVP 片段(视图),它托管 RecyclerView 适配器 (RecyclerViewAdapter) 的自定义实现。此适配器扩展了我的自定义 RecyclerViewAdapterBase 适配器。由于托管 Fragment 的 Presenter 需要知道当前建模的数据是什么,因此 RecyclerView 将使用的数据作为私有字段存储在 Presenter 中。
也就是说,我目前正在通过 Adapter 的构造函数将 Presenter 传递给 RecyclerViewAdapter。这有风险吗?
我能看到的唯一风险是 RecyclerViewAdapter 比 Fragment 长,因此比 Presenter 长。但是,这是不可能的,因为 Fragment 持有对 Adapter 和 Presenter 的私有引用,并且两者将同时被销毁。
话虽这么说,我想过只在构造函数中将所需的数据传递给 RecyclerViewAdapter,但是我不确定如何在不引用 Presenter 的情况下更新数据? (数据当前建模的位置。)
谢谢你的想法!
Presenter hosting the Fragment needs to know what data is currently modelled
您只需传递要呈现的数据,不要将其存储在呈现器中。
I am currently passing the Presenter to the RecyclerViewAdapter through the Adapter's constructor. Is this a risk?
没有。 Adapter 只是一个对象,它不像 Activity
或 Thread
那样有自己的生命周期。如果 activity 被销毁 -> 适配器被销毁。如果适配器被销毁 -> 演示器被销毁。此处不漏,你真好
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?
取决于您使用演示器的频率。如果你只需要绘制一次,那么你可以创建一个演示者,使用它一次,从而避免对演示者的引用。但是,你有一个代表许多视图的适配器,所以,我很确定你会画很多东西。因此,请从您的适配器中保留对演示者的引用,这绝对没问题。这里没有内存和性能问题。
我有一个 MVP 片段(视图),它托管 RecyclerView 适配器 (RecyclerViewAdapter) 的自定义实现。此适配器扩展了我的自定义 RecyclerViewAdapterBase 适配器。由于托管 Fragment 的 Presenter 需要知道当前建模的数据是什么,因此 RecyclerView 将使用的数据作为私有字段存储在 Presenter 中。
也就是说,我目前正在通过 Adapter 的构造函数将 Presenter 传递给 RecyclerViewAdapter。这有风险吗?
我能看到的唯一风险是 RecyclerViewAdapter 比 Fragment 长,因此比 Presenter 长。但是,这是不可能的,因为 Fragment 持有对 Adapter 和 Presenter 的私有引用,并且两者将同时被销毁。
话虽这么说,我想过只在构造函数中将所需的数据传递给 RecyclerViewAdapter,但是我不确定如何在不引用 Presenter 的情况下更新数据? (数据当前建模的位置。)
谢谢你的想法!
Presenter hosting the Fragment needs to know what data is currently modelled
您只需传递要呈现的数据,不要将其存储在呈现器中。
I am currently passing the Presenter to the RecyclerViewAdapter through the Adapter's constructor. Is this a risk?
没有。 Adapter 只是一个对象,它不像 Activity
或 Thread
那样有自己的生命周期。如果 activity 被销毁 -> 适配器被销毁。如果适配器被销毁 -> 演示器被销毁。此处不漏,你真好
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?
取决于您使用演示器的频率。如果你只需要绘制一次,那么你可以创建一个演示者,使用它一次,从而避免对演示者的引用。但是,你有一个代表许多视图的适配器,所以,我很确定你会画很多东西。因此,请从您的适配器中保留对演示者的引用,这绝对没问题。这里没有内存和性能问题。