RecyclerView 多布局使用

开发中列表布局显示根据数据类型显示不同的布局

使用 BaseMultiItemQuickAdapter

1.实体类

package cn.xxx.bean;

import android.text.TextUtils;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import cn.elevator.config.Constant;
import cn.elevator.utils.SharedPrefUtils;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;

/**
 * Created by Yangzb on 2019/1/23 13:57
 * E-mail:[email protected]
 * Describe:
 */
@Entity
public class FormListData implements MultiItemEntity {
    public static final int TEXT_BG_BLUE = 1;
    public static final int TEXT = 2;
    public static final int ITEM_EDIT = 3;
    /**
     * CheckProjectListID : 01201812110002-73
     * CheckRecordID : 01201812110002
     * ReportClassID : 1101
     * FatherId : 110104
     * CheckListCode : 11010402
     * CheckListName : 4.2扶手防爬/阻挡/防滑行装置
     * ListType :
     * InspectionItem : null
     * DefaultResult :
     * DefaultConclusion :
     * IFMergerConclusions : null
     * IFMergerConclusionsDefault : null
     * Remark : null
     */

    @Id(assignable = true)
    private long id;
    private String UserId;
    private String CheckProjectListID;
    private String CheckRecordID;
    private String ReportClassID;
    private String FatherId;
    private String CheckListCode;
    private String CheckListName;
    private String ListType;
    private String InspectionItem;
    private String DefaultResult;
    private String DefaultConclusion;
    private int IFMergerConclusions;
    private int IFMergerConclusionsDefault;
    private String Remark;
    private int ord;

    @Override
    public int getItemType() {
        if(FatherId.length()==4){
            return TEXT_BG_BLUE;
        }else if(!TextUtils.isEmpty(DefaultResult)){
            return ITEM_EDIT;
        }
        return TEXT;
    }

    public long getId() {
        return Long.valueOf(this.CheckProjectListID);
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUserId() {
        return SharedPrefUtils.getObj(Constant.USERID);
    }

    public void setUserId(String userId) {
        UserId = userId;
    }

    public String getCheckProjectListID() {
        return CheckProjectListID;
    }

    public void setCheckProjectListID(String CheckProjectListID) {
        this.CheckProjectListID = CheckProjectListID;
    }

    public String getCheckRecordID() {
        return CheckRecordID;
    }

    public void setCheckRecordID(String CheckRecordID) {
        this.CheckRecordID = CheckRecordID;
    }

    public String getReportClassID() {
        return ReportClassID;
    }

    public void setReportClassID(String ReportClassID) {
        this.ReportClassID = ReportClassID;
    }

    public String getFatherId() {
        return FatherId;
    }

    public void setFatherId(String FatherId) {
        this.FatherId = FatherId;
    }

    public String getCheckListCode() {
        return CheckListCode;
    }

    public void setCheckListCode(String CheckListCode) {
        this.CheckListCode = CheckListCode;
    }

    public String getCheckListName() {
        return CheckListName;
    }

    public void setCheckListName(String CheckListName) {
        this.CheckListName = CheckListName;
    }

    public String getListType() {
        return ListType;
    }

    public void setListType(String ListType) {
        this.ListType = ListType;
    }

    public String getInspectionItem() {
        return InspectionItem;
    }

    public void setInspectionItem(String InspectionItem) {
        this.InspectionItem = InspectionItem;
    }

    public String getDefaultResult() {
        return DefaultResult;
    }

    public void setDefaultResult(String DefaultResult) {
        this.DefaultResult = DefaultResult;
    }

    public String getDefaultConclusion() {
        return DefaultConclusion;
    }

    public void setDefaultConclusion(String DefaultConclusion) {
        this.DefaultConclusion = DefaultConclusion;
    }

    public int getIFMergerConclusions() {
        return IFMergerConclusions;
    }

    public void setIFMergerConclusions(int IFMergerConclusions) {
        this.IFMergerConclusions = IFMergerConclusions;
    }

    public int getIFMergerConclusionsDefault() {
        return IFMergerConclusionsDefault;
    }

    public void setIFMergerConclusionsDefault(int IFMergerConclusionsDefault) {
        this.IFMergerConclusionsDefault = IFMergerConclusionsDefault;
    }

    public String getRemark() {
        return Remark;
    }

    public void setRemark(String Remark) {
        this.Remark = Remark;
    }

    public int getOrd() {
        return ord;
    }

    public void setOrd(int ord) {
        this.ord = ord;
    }
}

2.adapter

package cn.xxx.ui.adapter;

import android.text.InputType;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.List;

import cn.elevator.R;
import cn.elevator.app.App;
import cn.elevator.bean.FormListData;
import cn.elevator.bean.FormListData_;
import cn.elevator.bean.TaskListData;
import cn.elevator.utils.ToastUtil;
import io.objectbox.Box;

/**
 * Created by Yangzb on 2019/1/23 14:22
 * E-mail:[email protected]
 * Describe:
 */
public class FormDataAdapter extends BaseMultiItemQuickAdapter {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    private String[] means = {"符合", "不符合", "无此项","资料确认符合"};
    private String[] results = {"合格", "不合格", "——"};

    public FormDataAdapter(List data) {
        super(data);
        addItemType(FormListData.TEXT_BG_BLUE, R.layout.item_text_bg_blue);
        addItemType(FormListData.TEXT, R.layout.item_text);
        addItemType(FormListData.ITEM_EDIT, R.layout.item_edit);
    }

    @Override
    protected void convert(BaseViewHolder helper, FormListData item) {
        switch (helper.getItemViewType()) {
            case FormListData.TEXT_BG_BLUE:
                helper.setText(R.id.id_tv_title, item.getCheckListName());
                LinearLayout show1 = helper.getView(R.id.id_ll_show);
                show1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showTip(item.getInspectionItem());
                    }
                });
                break;
            case FormListData.TEXT:
                helper.setText(R.id.id_tv_title, item.getCheckListName());
                LinearLayout show2 = helper.getView(R.id.id_ll_show);
                show2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showTip(item.getInspectionItem());
                    }
                });
                break;
            case FormListData.ITEM_EDIT:
                helper.setText(R.id.id_tv_title, item.getCheckListName());
                helper.setText(R.id.id_tv_type, item.getListType());
                TextView check = helper.getView(R.id.id_tv_check);
                TextView result = helper.getView(R.id.id_tv_result);
                check.setText(item.getDefaultResult());
                result.setText(item.getDefaultConclusion());
                LinearLayout show3 = helper.getView(R.id.id_ll_show);
                show3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showTip(item.getInspectionItem());
                    }
                });
                check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showMeans(check, item);

                    }
                });
                check.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        //长按填写
                        showEdit(check, item);
                        return true;
                    }
                });
                result.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showResults(result, item);
                    }
                });
                break;
        }
    }

    private void showEdit(TextView check, FormListData item) {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(mContext);
        builder.setTitle("输入").setPlaceholder("在此输入检验结果").
                setInputType(InputType.TYPE_CLASS_TEXT).
                addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            item.setDefaultResult(text.toString());
                            Box listDataBox = App.getInstance().
                                    getBoxStore().boxFor(FormListData.class);
                            listDataBox.put(item);
                            notifyDataSetChanged();
                            dialog.dismiss();
                        } else {
                            ToastUtil.showToast(mContext,"请输入检验结果");
                        }
                    }
                }).show();
    }


    private void showMeans(TextView v, FormListData item) {
        QMUIDialog.MenuDialogBuilder typeBuilder = new QMUIDialog.MenuDialogBuilder(mContext).
                addItems(means, (dialog, which) -> {
                    dialog.dismiss();
//                    v.setText(means[which]);
                    if(!item.getListType().equals("C") && which==3){
                        ToastUtil.showToast(mContext,"此项不能选择资料确认符合");
                        return;
                    }
                    item.setDefaultResult(means[which]);
                    if (item.getIFMergerConclusions() == 0) {//不是合并项
                        if(item.getListType().equals("C")){
                            if(which==0 || which==3){
                                item.setDefaultConclusion(results[0]);
                            }else {
                                item.setDefaultConclusion(results[which]);
                            }
                        }else {
                            item.setDefaultConclusion(results[which]);
                        }
                        Box listDataBox = App.getInstance().
                                getBoxStore().boxFor(FormListData.class);
                        listDataBox.put(item);
                        notifyDataSetChanged();
                    } else {//是合并项
                        Box listDataBox = App.getInstance().
                                getBoxStore().boxFor(FormListData.class);
                        listDataBox.put(item);
                        notifyDataSetChanged();
                        List listDatas = listDataBox.find(FormListData_.FatherId, item.getFatherId());
                        int index = -1;//合格/不合格
//                        int position = 2;//无此项
                        for (FormListData data : listDatas){
                            if(data.getDefaultResult().equals(means[1])){
                                index = 1;//不合格
                                break;
                            }else if(data.getDefaultResult().equals(means[0]) || data.getDefaultResult().equals(means[3])){
                                index = 0;//合格
                            }
//                            else if(data.getDefaultResult().equals(means[2])){
//                                position = 2;//无此项
//                            }
                        }
                        for (FormListData data:listDatas){
                            if(data.getIFMergerConclusionsDefault()==1){//显示合并结论
                                if(index > -1){
                                    data.setDefaultConclusion(results[index]);
                                }else {
                                    data.setDefaultConclusion(results[2]);
                                }
                                listDataBox.put(data);
                                notifyDataSetChanged();
                                break;
                            }
                        }
                    }
//

                });
        QMUIDialog typeDialog = typeBuilder.create();
        typeDialog.show();
    }

    private void showResults(TextView v, FormListData item) {
        QMUIDialog.MenuDialogBuilder typeBuilder = new QMUIDialog.MenuDialogBuilder(mContext).
                addItems(results, (dialog, which) -> {
                    dialog.dismiss();
//                    v.setText(results[which]);
                    item.setDefaultConclusion(results[which]);
                    Box listDataBox = App.getInstance().
                            getBoxStore().boxFor(FormListData.class);
                    listDataBox.put(item);
                    notifyDataSetChanged();
                });
        QMUIDialog typeDialog = typeBuilder.create();
        typeDialog.show();
    }

    private void showTip(String inspectionItem) {
        new QMUIDialog.MessageDialogBuilder(mContext)
                .setTitle("描述")
                .setMessage(inspectionItem)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .show();
    }
}

注:

QMUIDialog是引用 腾讯ui框架 QMUI 可自行查阅

BaseMultiItemQuickAdapter引用BaseRecyclerViewAdapterHelper

3.给RecyclerView设置adapter即可

你可能感兴趣的:(Android,RecyclerView,多布局)