Android – add clip in recyclerview.viewholder

I have a recyclerview.viewholder, which will add different fragments to its FrameLayout according to the instance of the passed object. The problem is that it is almost impossible to add fragments to the viewholder. Please note that I have passed the fragmentmanager from the parent. Initially, I tried to use 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();
    }
}

The problem with this code is that all viewholders share the same ID, so only one viewholder can add this fragment. In my recyclerview, only the first cell adds a fragment. To solve this problem, I created another FrameLayout and added it to typecontainerlayout. Now my code is 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();
    }
}

Now each viewholder has correctly added a clip and has its own clip. However, when I add five viewholders and try to scroll down recyclerview, there is a problem and an error state occurs at runtime

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)

My guess is that due to the viewholder mode, the ID conflicts sometimes, or the view is damaged. So my problem is

1) Is there a solution?

2) Is there a better way than adding fragments? The reason why I add fragments is that the logic of viewholder subitems can be located in a single fragment. Of course, I can put two views of fragments into viewholder XML. And setvisible() only depends on the condition. But this will only make my viewholder contain too much logic

If someone is confused, why do I need clips? That's what I want to achieve

resolvent:

Short answer: you shouldn't use clips in recyclerview. That's not their purpose

The answer is long: here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>