Android MVP - 在 View 实现中调用 View 方法(Fragements 或 Activity)
Android MVP - Calling View method inside View implementation (Fragements or Activity)
我在我的新应用程序中实施 MVP,然后我遇到了一个问题。我需要在 View
(Activity
) 本身内部调用 View
的方法。根据 MVP 代码分离的定义,这是错误的做法。
根据定义:
Presenter负责Model和View之间的编排。它基本上从两者接收事件并据此采取行动。 Presenter 是唯一知道其他组件的组件。它有一个对视图的引用和另一个对模型的引用。 (source)
在同一篇文章中提到 View
不会对用户交互作出反应,它将控制权交给 Presenter
来完成这项工作。我还阅读了 关于依赖规则的内容。
就我而言,我使用的是自定义 AppTheme
。 AppTheme
需要在 setContent()
调用之前设置,我正在做的是在 View
接口中创建一个名为 setAppTheme()
的方法,我的 Activity
实现了该方法,并且有应用主题的代码。现在的问题是,这是在应用程序中调用的,它在其实现中调用了 View
方法。
To sum up, what my understanding of MVP either one of the following
should be true:
Do call View
method inside Activity
, because setTheme()
wont work after setContent()
and our presenter.setView()
is in
onResume()
, but will this satisfy MVP separation of M-V-P ?
Do not make interface method for setAppTheme()
, instead create a private method in Activity
which sets theme. This method will have
nothing to do with any layer of MVP. But question is, if project is
using MVP pattern, is this practice valid?
这是我的 MVP:
public class AboutUsMVP
{
public interface Model
{
String getFbLink();
String getTwitterLink();
String getEmailLink();
String getCompanyLink();
}
public interface Presenter
{
void setView( View view );
void fbButtonClicked();
void twitterButtonClicked();
void emailButtonClicked();
void imageButtonClicked();
}
public interface View
{
void showFacebookPage();
void showTwitterPage();
void showEmailIntent();
void showCompanyWebsite();
void setAppTheme();
void setCustomActionBar();
}
}
请指出我遗漏的错误。
From what I know, same case can be argued in the light of
setActionBar()
and setOnClickListener()
methods, although these
may require their separate post, but they are more relevant here and
new post for either of them will be duplicate.
请注意,我的 Activity 实现了 View 接口。
编辑:更多解释
我的View其实是Activityclass。这是MVP的看法,不是AndroidAPI的View
class.问题是,有一个方法 setAppTheme()
只与 MVP 的视图相关,(Activity
of Android)。此调用不在合约 (AboutUsMVP.java
) 中,根据 Google 约定应为 AboutUsContract.java
,此 setAppTheme()
不在合约中,也不可能,这是否违反MVP原则?
没有可能的选择,可以说做一个 setAppTheme()
的接口,如果我这样做,它不会工作,因为:
setAppTheme()
紧接在 super()
方法之后被调用,否则它是无用的。 MVP 的主持人开始在 onResume
工作。如果做了一个接口,setAppTheme()
被MVP管辖,就没有效果了
事实上,MVP 中的 View
应该是 愚蠢的 。这是:它们不包含任何逻辑。只需接收用户生成的事件并立即将其工作委托给演示者。 View
还可以通知演示者一些事件 已经发生(视图已创建、屏幕旋转等)
这可能会导致一些混淆。谁负责调用某些方法?正如您所说,View
有 执行某些操作,例如 setOnClickListener
,但演示者负责处理事件。请记住这一点:
The View
is just an interface. This means that you can use anything that implements that interface
现在您正在制作移动应用程序。但是,如果您想编写控制台或桌面应用程序代码,则表示逻辑 不会改变 。任何特定于 "View technology"(android、桌面等)的内容都应在特定于该技术的代码中执行。这样你的代码就会松耦合到你的技术堆栈
我相信@Pelocho 的回答是正确的,您应该将其标记为正确的。但我想从另一个角度提出我的答案(同意他的答案)。关于这个不同的 POV,我想讨论你对主持人的定义:
The Presenter is in charge of the the orchestration between the Model
and the View
所以我在这里提出的问题是:"what is this model that the presenter interacts with?"。我对此的回答是研究经典的 "note-taking" 应用程序,并认为模型是文本和关联的元数据,通常存储在演示者将读取、解析并推送到视图的某些数据库中。
在此 "model" 中与应用程序主题无关。主题纯粹是视图的功能。主题仅与它的最终屏幕外观相关联。就像视图负责在屏幕上布局内容,或者知道使用什么字体大小一样。
这意味着即使可以从用户设置选项更改布局、大小、颜色,它仍然不在演示者和模型的责任范围内,因为他们只对内容感兴趣。
tl;博士:
直接从 SharedPreferences 中阅读 Activity.onCreate
(在 super.onCreate 之前)的主题,不要让 Presenter 参与其中。
我在我的新应用程序中实施 MVP,然后我遇到了一个问题。我需要在 View
(Activity
) 本身内部调用 View
的方法。根据 MVP 代码分离的定义,这是错误的做法。
根据定义:
Presenter负责Model和View之间的编排。它基本上从两者接收事件并据此采取行动。 Presenter 是唯一知道其他组件的组件。它有一个对视图的引用和另一个对模型的引用。 (source)
在同一篇文章中提到 View
不会对用户交互作出反应,它将控制权交给 Presenter
来完成这项工作。我还阅读了
就我而言,我使用的是自定义 AppTheme
。 AppTheme
需要在 setContent()
调用之前设置,我正在做的是在 View
接口中创建一个名为 setAppTheme()
的方法,我的 Activity
实现了该方法,并且有应用主题的代码。现在的问题是,这是在应用程序中调用的,它在其实现中调用了 View
方法。
To sum up, what my understanding of MVP either one of the following should be true:
Do call
View
method insideActivity
, becausesetTheme()
wont work aftersetContent()
and ourpresenter.setView()
is inonResume()
, but will this satisfy MVP separation of M-V-P ?Do not make interface method for
setAppTheme()
, instead create a private method inActivity
which sets theme. This method will have nothing to do with any layer of MVP. But question is, if project is using MVP pattern, is this practice valid?
这是我的 MVP:
public class AboutUsMVP
{
public interface Model
{
String getFbLink();
String getTwitterLink();
String getEmailLink();
String getCompanyLink();
}
public interface Presenter
{
void setView( View view );
void fbButtonClicked();
void twitterButtonClicked();
void emailButtonClicked();
void imageButtonClicked();
}
public interface View
{
void showFacebookPage();
void showTwitterPage();
void showEmailIntent();
void showCompanyWebsite();
void setAppTheme();
void setCustomActionBar();
}
}
请指出我遗漏的错误。
From what I know, same case can be argued in the light of
setActionBar()
andsetOnClickListener()
methods, although these may require their separate post, but they are more relevant here and new post for either of them will be duplicate.
请注意,我的 Activity 实现了 View 接口。
编辑:更多解释
我的View其实是Activityclass。这是MVP的看法,不是AndroidAPI的View
class.问题是,有一个方法 setAppTheme()
只与 MVP 的视图相关,(Activity
of Android)。此调用不在合约 (AboutUsMVP.java
) 中,根据 Google 约定应为 AboutUsContract.java
,此 setAppTheme()
不在合约中,也不可能,这是否违反MVP原则?
没有可能的选择,可以说做一个 setAppTheme()
的接口,如果我这样做,它不会工作,因为:
setAppTheme()
紧接在 super()
方法之后被调用,否则它是无用的。 MVP 的主持人开始在 onResume
工作。如果做了一个接口,setAppTheme()
被MVP管辖,就没有效果了
事实上,MVP 中的 View
应该是 愚蠢的 。这是:它们不包含任何逻辑。只需接收用户生成的事件并立即将其工作委托给演示者。 View
还可以通知演示者一些事件 已经发生(视图已创建、屏幕旋转等)
这可能会导致一些混淆。谁负责调用某些方法?正如您所说,View
有 执行某些操作,例如 setOnClickListener
,但演示者负责处理事件。请记住这一点:
The
View
is just an interface. This means that you can use anything that implements that interface
现在您正在制作移动应用程序。但是,如果您想编写控制台或桌面应用程序代码,则表示逻辑 不会改变 。任何特定于 "View technology"(android、桌面等)的内容都应在特定于该技术的代码中执行。这样你的代码就会松耦合到你的技术堆栈
我相信@Pelocho 的回答是正确的,您应该将其标记为正确的。但我想从另一个角度提出我的答案(同意他的答案)。关于这个不同的 POV,我想讨论你对主持人的定义:
The Presenter is in charge of the the orchestration between the Model and the View
所以我在这里提出的问题是:"what is this model that the presenter interacts with?"。我对此的回答是研究经典的 "note-taking" 应用程序,并认为模型是文本和关联的元数据,通常存储在演示者将读取、解析并推送到视图的某些数据库中。
在此 "model" 中与应用程序主题无关。主题纯粹是视图的功能。主题仅与它的最终屏幕外观相关联。就像视图负责在屏幕上布局内容,或者知道使用什么字体大小一样。
这意味着即使可以从用户设置选项更改布局、大小、颜色,它仍然不在演示者和模型的责任范围内,因为他们只对内容感兴趣。
tl;博士:
直接从 SharedPreferences 中阅读 Activity.onCreate
(在 super.onCreate 之前)的主题,不要让 Presenter 参与其中。