简体   繁体   中英

Adding multiple different CardViews to RecyclerView

I have a RecyclerView and I want to add to that RecyclerView multiple different CardViews . Lets say I have cardview1.xml , cardview2.xml , cardview3.xml each ViewCard has Class that have the variables for the ViewCard and the Classes extends from Class called CardView that I created with no properties.

I want to add to one RecyclerView 3 cardview1 , 2 cardview2 , 4 cardview3 .

I want to know how to do that in My Adapter

CardView1:

public class CardView1 extends CardView {

    public CardView1(){

    }

}

CardView2:

public class CardView2 extends CardView {

    public CardView2(){

    }
}

CardView3:

public class CardView3 extends CardView {
    private String number;

    public CardView3(String number){
        this.number = number;
    }

    public String getNumber() {
        return number;
    }

}

If you need me to add any code please tell.

You can pass an ArrayList to your Adapter. This ArrayList represent a list of your data. In your adapter you have just to know with getType() which cardView to show. So for example:

YourAdapter(datas ArrayList<YourData>(),...){
onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int){
     val type = getItemViewType(position)
     if(type == 0){
        //show cardview1
         YourViewHolder(LayoutInflater.from(parent.context)
                    .inflate(R.layout.cardview1, parent, false))
     }
     if(type == 1){
          YourViewHolder(LayoutInflater.from(parent.context)
                    .inflate(R.layout.cardview2, parent, false))
     }

}
@override
public Int getItemViewType(int position){
            
            
            return datas[position].type;
             
        }
}

After YourData has to have a method type ( for this example). And you can add 3 elements of type 0, after 2 elements of type 1... in the arraylist.

Sorry because there's a mix between kotlin and Java ( but I think you can understand)

You can achieve it by this way

Firstly Add 3 Integer variables in your Model class name type ITEM_TYPE_1 and ITEM_TYPE_2. Assign a value to type According to your need. like ITEM_TYPE_1 or ITEM_TYPE_2 respectively for view type 1 and view type 2.

public class Model{
        String SmsAddress;
        String KoreaImage;
        String seasonNumber;
        Integer type;
        public  static final Integer ITEM_TYPE_1=1;
        public  static final Integer ITEM_TYPE_2=2;

        public String getSmsAddress() {
            return SmsAddress;
        }

        public void setSmsAddress(String smsAddress) {
            SmsAddress = smsAddress;
        }

        public String getKoreaImage() {
            return KoreaImage;
        }

        public void setKoreaImage(String koreaImage) {
            KoreaImage = koreaImage;
        }

        public String getSeasonNumber() {
            return seasonNumber;
        }

        public void setSeasonNumber(String seasonNumber) {
            this.seasonNumber = seasonNumber;
        }

        public Integer getType() {
            return type;
        }

        public void setType(Integer type) {
            this.type = type;
        }
    }

At your Adapter you can override getItemViewType() and onCreateViewHolder () methods and apply a switch case for view inflation and Data Presentation through ViewHolders.

@Override
    public int getItemViewType(int position) {
        switch (modelList.get(position).getType()) {
            case 0:
                return Model.ITEM_TYPE_1;
            case 1:
                return Model.ITEM_TYPE_2;
            default:
                return -1;
        }
    }

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    switch (viewType) {
        case Model.ITEM_TYPE_1:
            view = LayoutInflater.from(mContext).inflate(R.layout.item_type_1, parent, false);
            return (new Item1ViewHolder(view));

        case Model.ITEM_TYPE_2:
            view = LayoutInflater.from(mContext).inflate(R.layout.item_type_2, parent, false);
            return (new Item2ViewHolder(view));
        default:
            return null;
    }
}

For More Detail see the link below:

See Detail

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