Android RecyclerView多布局超详细

首先根据你的需求需要几个布局就先定义几个常量,用来表示相对应的布局:

再重写一个getItemViewType()的方法,用来标记在下标为几的时候返回上面对应的常量标记:

Android RecyclerView多布局超详细_第1张图片

还要说明的就是onCreateViewHolder()方法,有几个布局就写几个对应的Holder:

Android RecyclerView多布局超详细_第2张图片

Android RecyclerView多布局超详细_第3张图片

最后就是onBindViewHolder()方法,分别在对应的布局写对应的操作:

Android RecyclerView多布局超详细_第4张图片

全部代码展示:

package com.jiuhao.jhjk.adapter.MyRecyclerAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiuhao.jhjk.R;
import com.jiuhao.jhjk.bean.DocpricetimeBean;

import java.util.List;

/**
 * Created by Administrator on 2019/8/31.
 * 咨询费列表
 */

public class ConsultingRecyclerAdapter extends RecyclerView.Adapter {

    private Context context;
    private List docpricetimeBeans;

    private int ONE = 0;//自定义
    private int TWO = 1;//免费
    private int THREE = 2;//几元/次

    public ConsultingRecyclerAdapter(Context context, List docpricetimeBeans) {
        this.context = context;
        this.docpricetimeBeans = docpricetimeBeans;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (getItemViewType(i) == ONE) {
            View view1 = LayoutInflater.from(context).inflate(R.layout.item_consutlting, viewGroup, false);
            return new OneHolder(view1);
        } else if (getItemViewType(i) == TWO) {
            View view2 = LayoutInflater.from(context).inflate(R.layout.item_consutling_two, viewGroup, false);
            return new TwoHolder(view2);
        } else if (getItemViewType(i) == THREE) {
            View view3 = LayoutInflater.from(context).inflate(R.layout.item_consutling_three, viewGroup, false);
            return new ThreeHolder(view3);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        if (viewHolder instanceof OneHolder) {

        } else if (viewHolder instanceof TwoHolder) {

        } else if (viewHolder instanceof ThreeHolder) {

            for (int j = 0; j < docpricetimeBeans.size(); j++) {
                int markType = docpricetimeBeans.get(j).getMarkType();
                if (markType == 2) {
                    int price = docpricetimeBeans.get(j).getPrice();
                    ((ThreeHolder) viewHolder).price.setText(price + "元/次");
                }
            }

        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == ONE) {
            return ONE;
        } else if (position == TWO) {
            return TWO;
        } else if (position == THREE) {
            return THREE;
        } else {
            return super.getItemViewType(position);
        }
    }

    class OneHolder extends RecyclerView.ViewHolder {

        public EditText set_money;
        public ImageView type_one;

        public OneHolder(@NonNull View itemView) {
            super(itemView);
            set_money = itemView.findViewById(R.id.set_money);
            type_one = itemView.findViewById(R.id.type_one);
        }
    }

    class TwoHolder extends RecyclerView.ViewHolder {

        public ImageView type_zero;

        public TwoHolder(@NonNull View itemView) {
            super(itemView);
            type_zero = itemView.findViewById(R.id.type_zero);
        }
    }

    class ThreeHolder extends RecyclerView.ViewHolder {

        public TextView price;
        public ImageView type_two;

        public ThreeHolder(@NonNull View itemView) {
            super(itemView);
            price = itemView.findViewById(R.id.price);
            type_two = itemView.findViewById(R.id.type_two);
        }
    }
}

 

你可能感兴趣的:(Android RecyclerView多布局超详细)