在 RecyclerView.ViewHolder 中添加片段 [英] Add fragment in RecyclerView.ViewHolder

查看:25
本文介绍了在 RecyclerView.ViewHolder 中添加片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RecyclerView.ViewHolder,它将根据传递的对象实例将不同的片段添加到其 FrameLayout 中.问题出现在几乎不可能将片段添加到 ViewHolder 的地方.请注意,我已经从父级传递了 FragmentManager.最初我尝试使用此代码

I have a RecyclerView.ViewHolder which will add different fragment into its FrameLayout based on the instance of the object passed. The problem comes where it is almost impossible to add fragment into the ViewHolder. Take note that I already passed the FragmentManager from the parent. Initially I try with this code

public void setSomething(boolean A) {
    if (A) {
         mFragmentManager.beginTransaction()
            .replace(mBinding.typeContainerLayout.getId(), new FragmentA())
            .commit();
    } else {
        mFragmentManager.beginTransaction()
            .replace(mBinding.typeContainerLayout.getId(), new FragmentB())
            .commit();
    }
}

这段代码的问题是所有的 ViewHolder 共享相同的 id,因此只有一个 ViewHolder 可以添加片段.在我的 RecyclerView 中,只有第一个单元格添加了片段.为了解决这个问题,我创建了另一个 FrameLayout 并将其添加到 typeContainerLayout 中.现在我的代码变成这样了.

The problem with this code is that all the ViewHolder share the same id, thus only a single ViewHolder can add the fragment. In my RecyclerView, only the first cell added the fragment. To tackle this problem, I create another FrameLayout and add it into typeContainerLayout. Now my code become like this.

public void setSomething(boolean A) {
    FrameLayout frameLayout = new FrameLayout(mContext);
    frameLayout.setId(View.generateViewId());
    mBinding.typeContainerLayout.removeAllViews();
    mBinding.typeContainerLayout.addView(frameLayout)

    if (A) {
         mFragmentManager.beginTransaction()
            .replace(frameLayout.getId(), new FragmentA())
            .commit();
    } else {
        mFragmentManager.beginTransaction()
            .replace(frameLayout.getId(), new FragmentB())
            .commit();
    }
}

现在每个 ViewHolder 都正确添加了片段并拥有自己的片段.然而,当我添加 5 个 ViewHolder 并尝试向下滚动 RecyclerView 时,问题出现了,在哪个状态下发生了运行时错误

Now each ViewHolder has added the fragment correctly and has their own fragment. However the problem comes when I added like 5 ViewHolder and trying to scroll down the RecyclerView, a runtime error occurred which state

java.lang.IllegalArgumentException: No view found for id 0x4 (unknown) for fragment FragmentA{7c55a69 #0 id=0x4 FragmentA}
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1292)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                      at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的猜测是 id 在某个时候发生了冲突,或者视图由于 ViewHolder 模式而被破坏.所以我的问题是.

My guess is that either the id conflicted at some point, or the view got destroyed due to the ViewHolder pattern. So my question is that.

1) 有什么解决方法吗?

1) Is there any workaround to it?

2) 有没有比添加片段更好的做法.我添加fragment的原因是为了ViewHolder的子项的逻辑可以全部位于单个fragment中.当然,我可以将片段的两个视图都放入 ViewHolder xml 中.并且只是 setVisible() 取决于条件.但这只会让我的 ViewHolder 包含太多逻辑.

2) Is there any better practice than adding fragment. The reason I add fragment is so that the logic for the sub item of the ViewHolder can all be located in a single fragment. Of course I can just put both the views for the fragments into the ViewHolder xml. And just setVisible() depending on the condition. But that will just make my ViewHolder contain too many logic.

以防有人对我为什么需要片段感到困惑.这就是我正在努力实现的目标.图片

In case someone is confused why I need fragment. This is what I am trying to achieve. The image

推荐答案

简短回答:您不应该在 recyclerView 中使用片段,这不是它们的用途.

Short answer: you shouldn't use fragments inside a recyclerView, that's not what they're intended for.

长答案:这里

这篇关于在 RecyclerView.ViewHolder 中添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆