RecyclerView onCreateViewHolder Return Type Incompatibility With Multiple Custom ViewHolders

前端 未结 2 1329
一向
一向 2020-12-18 19:57

I\'m trying to use multiple ViewHolders in a RecyclerView in order to swap these views out at run time. I have created two classes which extend RecyclerView.ViewHolder:

相关标签:
2条回答
  • 2020-12-18 20:28

    Full working code

    For those who want a full working code. There you go.

     public class YourListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
          private static final int LAYOUT_ONE= 0;
          private static final int LAYOUT_TWO= 1;
    
          @Override
          public int getItemViewType(int position)
          {
              if(position==0)
                 return LAYOUT_ONE;
              else
                 return LAYOUT_TWO;
          }
    
          @Override
          public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
             View view =null;
             RecyclerView.ViewHolder viewHolder = null;
    
             if(viewType==LAYOUT_ONE)
             {
                 view = LayoutInflater.from(parent.getContext()).inflate(R.layout.one,parent,false);
                viewHolder = new ViewHolderOne(view);
             }
             else
             {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.two,parent,false);
                viewHolder= new ViewHolderTwo(view);
             }
    
            return viewHolder;
    
          }
    
          @Override
          public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    
              if(holder.getItemViewType()== LAYOUT_ONE)
              {
                  // Typecast Viewholder 
                  // Set Viewholder properties 
                  // Add any click listener if any 
              }
              else {
    
                  ViewHolderOne vaultItemHolder = (ViewHolderOne) holder;
                  vaultItemHolder.name.setText(displayText);
                  vaultItemHolder.name.setOnClickListener(new View.OnClickListener() {
    
                         @Override
                         public void onClick(View v) {
                          .......
                         }
                    });
    
                }
    
            }
    
         //****************  VIEW HOLDER 1 ******************//
    
          public class ViewHolderOne extends RecyclerView.ViewHolder {
    
              public TextView name;
    
              public ViewHolderOne(View itemView) {
                   super(itemView);
                   name = (TextView)itemView.findViewById(R.id.displayName);
              }
          }
    
    
       //****************  VIEW HOLDER 2 ******************//
    
          public class ViewHolderTwo extends RecyclerView.ViewHolder{
    
              public ViewHolderTwo(View itemView) {
              super(itemView);
    
               ..... Do something
          }
       }
    }
    

    Read more

    You can read more about creating RecyclerView with multiple ViewHolders

    0 讨论(0)
  • 2020-12-18 20:31

    The RecyclerView.Adapter is a parameterized class that takes in a single ViewHolder type. Your definition probably looks like:

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MenuItemViewHolder>
    

    It should be:

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
    

    Don't forget to have the right definition for onBindViewHolder as well. You need to override getItemViewType as well.

    0 讨论(0)
提交回复
热议问题