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

查看:94
本文介绍了在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)有没有比添加片段更好的方法了?我添加片段的原因是为了使ViewHolder子项的逻辑都可以位于一个片段中.当然,我可以将片段的两个视图都放入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天全站免登陆