- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Mosby 实现一个 MVP Android View(不是 Activity 或 fragment ),但是当在 Android Adaptor 中使用该 View 并在 onBindViewHolder 中访问它时,演示者此时未初始化。似乎在 onBindViewHolder 完成之后才调用 onAttachWindow,因为演示者为 Null。这是我创建的抽象类。
public abstract class MvpImageView<V extends MvpView, P extends MvpPresenter<V>>
extends AppCompatImageView implements MvpView, ViewGroupDelegateCallback<V, P> {
protected P presenter;
protected ViewGroupMvpDelegate<V, P> mvpDelegate;
private boolean retainInstance = false;
public MvpImageView(Context context) {
super(context);
}
public MvpImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MvpImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* Get the mvp delegate. This is internally used for creating presenter, attaching and detaching
* view from presenter etc.
*
* <p><b>Please note that only one instance of mvp delegate should be used per android.view.View
* instance</b>.
* </p>
*
* <p>
* Only override this method if you really know what you are doing.
* </p>
*
* @return {@link ViewGroupMvpDelegate}
*/
@NonNull protected ViewGroupMvpDelegate<V, P> getMvpDelegate() {
if (mvpDelegate == null) {
mvpDelegate = new ViewGroupMvpDelegateImpl<>(this, this, true);
}
return mvpDelegate;
}
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.d(getClass().getName(), "Attaching to Window");
getMvpDelegate().onAttachedToWindow();
}
@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Log.d(getClass().getName(), "Detaching from Window");
getMvpDelegate().onDetachedFromWindow();
}
@SuppressLint("MissingSuperCall") @Override protected Parcelable onSaveInstanceState() {
return getMvpDelegate().onSaveInstanceState();
}
@SuppressLint("MissingSuperCall") @Override
protected void onRestoreInstanceState(Parcelable state) {
getMvpDelegate().onRestoreInstanceState(state);
}
/**
* Instantiate a presenter instance
*
* @return The {@link MvpPresenter} for this view
*/
public abstract P createPresenter();
@Override public P getPresenter() {
return presenter;
}
@Override public void setPresenter(P presenter) {
this.presenter = presenter;
}
@Override public V getMvpView() {
return (V) this;
}
@Override public final Parcelable superOnSaveInstanceState() {
return super.onSaveInstanceState();
}
@Override public final void superOnRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
}
}
这是基于 MvpLinearLayout 实现。我过去使用过另一个 MVP 库,称为 Moxy,它具有用于 onCreate 和 onAttachToWindow 的委托(delegate)方法。
我可以添加一个在构造函数中调用 getMvpDelegate().onAttachWindow 的初始化例程,但这看起来更像是一个 hack,而不是其他任何东西。关于在 onBindViewHolder 和 RecyclerView 中使用时如何使它工作的任何想法?
最佳答案
不要在您的适配器中使用 MVP。它只会让事情变得复杂和繁琐。
例子:Views/ViewHolder 可以回收,Presenter 也应该回收吗?如果是,您如何实现并确保内部 Presenter 是最新的?如何在滚动时重新附加 Presenter 以查看?如果您的 AdapterSet 被更改怎么办?屏幕方向发生变化怎么办?
千万不要那样做。
关于android - 使用 Mosby 实现非 ViewGroup MVP Android View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47715948/
我有一个名为 Customview 的 viewGroup。我需要在运行时将另一个具有多个 subview 的 viewGroup 分配给 Customview。如果以上一个是可能的,我没有正确获取
我有一个包含 N 个子项的自定义 ViewGroup。现在,我创建了另一组 ViewGroup。我想将第二个 ViewGroup 添加到第一个。我怎么做?谢谢.. 最佳答案 来自代码: firstVi
我遇到了一个奇怪的问题。例如,我们有这样的布局: 我们的屏幕将变黑。如果我将 LinearLayout 替换为 FrameLayout 或 ConstraintLayout,结果相同
我希望能够以编程方式将一个 View 组添加到另一个 View 组。两者都在 xml 中定义如下,连同我的 onCreate 方法: 主.xml: secondlayout.xml:
我不确定我的问题是否可以理解。我的布局中有一个 Image 和一个 ViewGroup。 ViewGroup 与 GridView 相当,我在其中有可以拖放的项目。现在我有兴趣将我的 ViewGrou
知道为什么会出现此崩溃吗?跟踪只有 native 代码,因此很难追踪到它。当我在 Fragment 中运行动画并且当动画结束时我为 FragmentStatePagerAdapter 调用 notif
在方法内部,我调用 addView和 removeView如下: public class EnclosingClass extends FrameLayout { ... void
如何检索以“llIndex”为 id 的 linearLayout 的索引。谢谢 最佳答案 非常简单 - 在 View 的父级上调用 indexOfChild 函数:
我有子元素(不是根)RelativeLayout 的布局,然后我创建 ViewPager 并将其作为我的相对布局的子元素插入,就像 +相对布局(根) | +-- 相对布局(子) | +-- -- 浏览
我有一个布局,其根 ViewGroup 有两个 child ,其中只有一个始终可见。如果不适用,另一个 child 的可见性将在运行时设置为 View.GONE。 当两个 child 都可见时,高度设
我的 XML 布局中有以下节点: TagLayout 是一个扩展 ViewGroup 的类,基本上包含一堆带有文本的按钮(一个自定义对象,充当标签,但它既不在这里也不在那里)。 问题是当按钮太多时,
如果我尝试使用 ViewGroup,我会收到此错误 “ViewHolder cannot be resolved to a type”。没有具体的import present,如何使用? 最佳答案 也
我的自定义 ViewGroup 有问题。我连续布置了 3 个 child ,并使用 Scroller 滚动到中间的 child 。根据用户的触摸输入,我更改了应该显示的子项并请求新布局。然后我按新顺序
我正在尝试在 Android 中制作简单的翻译动画。以下不起作用: public class MyView extends ViewGroup { ... TranslateAnima
为什么 android:clipChildren="false"不工作?我想实现与 CSS overflow: visible 相同的效果,这样即使 subview 位于其父布局之外,它们也是可见的。
假设有一个包含多个子项的 ViewGroup。至于这个 ViewGroup,我想让它管理所有子项的所有 MotionEvent,这表示 VG 将 1. 能够在事件被发送到目标( child )之前拦截
我有 CustomComponent 类,它扩展了 ViewGroup。 CustomComponent源代码: public class CustomComponent extends
我的 Android 应用程序中有几个具有相同基本结构的 Activity,我正试图让我的布局变干。重复的代码如下所示。它包含一个带有页脚的可滚动区域,该页脚具有“后退”和“仪表板”链接。还有一个 F
我有一个 Android TV 布局,如下图所示。 顶部一个PagerSlidingTabStrip,从服务器拉取数据; 底部有 Fragment 的 ViewPager,每个 Fragment 都有
我正在尝试创建带有内部包含文本的指示器的 fragment slider 。 我在网上找到了一些包含图像 slider 的教程,并将其编辑为文本 slider ,但我收到了此错误消息: android
我是一名优秀的程序员,十分优秀!