简体   繁体   中英

Implementing multiple viewholders and views in the most efficient way

I have some doubts on how to implement viewholders that I need for my app using C#.

I have three fragments and each one of them implements a recyclerview, let's call them 1, 2, 3 and 4. I have four different types of object: A, B, C, and D. They're all stored in the same collection.

Fragment 1's recycler view should cointain all of them.

Fragment 2's should just contain A.

Fragment 3's should just contain B.

Fragment 4's should just contain C and D.

As you can see multiple fragments end up containing the same kind of object. Now I'm wondering, is it better to have a viewholder stored in a folder on its own for let's say object A so that 1 and 2 can access it or is it better to implement two viewholders for each fragment, even though these viewholders would be same?

Now let's say A and B have the same exact layout structure but behave in different ways when interacted with, I'm 100% sure A and B will always have the same layout structure. Is it better to have two viewholders and make the fragment check what kind of object I got from the collection so it can pick the right viewholder, or is it better to have a generic single viewholder that can be used for both A and B and then eventually check if it's holding either A or B and implement their behaviour.

Let's say the generic viewholder option is the better, C is similar to A and B, but not the same, they share 80% of the layout. Is it still recommendable to have a generic viewholder here? C has extra views it needs to show which A and B plain don't have. Would it be wise to have a layout that has these extra views set to gone which then turn visible in case I have C and stay gone if I have A and B? Does the fact that A and B have these extra elements in the layout in a forever GONE status affect performance? Sorry, this part is more of a question about how the state GONE interacts with the memory than anything.

Sorry for the wall of text, also this is a question only about performance, not about which architecture would be more useful to work on from a developer's point of view.

You need to use something like this

For Fragment 1

Your RecyclerView adapter should have something like this

private final int objA = 1;
private final int objB = 2;
private final int objC = 3;
private final int objD = 4;

@Override
public int getItemViewType(int position) {
   Object obj = list.get(position);
   if(obj instanceof A){
       return objA;
    }      
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
   int type = getItemViewType(position);
   if(type==objA){
      //then show the object
   }
}

It is better to use 1 ViewHolder for each RecyclerView Adapter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM