OneClickDeliveryReAdapter.java 4.7 KB
package com.hh.xuetubao.adapter;

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.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.hh.xuetubao.R;
import com.hh.xuetubao.bean.HotJobBean;

import java.util.List;

public class OneClickDeliveryReAdapter extends RecyclerView.Adapter<OneClickDeliveryReAdapter.ViewHolder> {
    private List<HotJobBean.JobListBean> list;
    private Context context;
    private boolean isSelectAll = false;
    private String allJobId = "";

    public OneClickDeliveryReAdapter(List<HotJobBean.JobListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.rv_oneclick_delivery, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        if (list.get(position).getJobName() != null)
            holder.title.setText(list.get(position).getJobName());
        if (list.get(position).getSalary() != null)
            holder.money.setText(list.get(position).getSalary());
        if (list.get(position).getExprience() != null)
            holder.experience.setText(list.get(position).getExprience());
        if (list.get(position).getEducation() != null)
            holder.education.setText(list.get(position).getEducation());

        if (isSelectAll) {
            holder.select.setChecked(true);

            if (list.size() > 1) {
                for (int i = 0; i < list.size(); i++) {
                    if (allJobId == null) {
                        allJobId += list.get(position).getJobOid();
                    } else {
                        allJobId += ",";
                        allJobId += list.get(position).getJobOid();
                    }
                }
                selectJob.selectJobId(true, allJobId);
                allJobId = "";
            }

        } else {
            holder.select.setChecked(false);

            selectJob.selectJobId(false, "");
        }

        holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectJob.selectJobId(true, list.get(position).getJobOid());
                } else {
                    selectJob.selectJobId(false, list.get(position).getJobOid());
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (jumpToDetail != null) {
                    jumpToDetail.turnToDetail(list.get(position).getJobOid());
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView title;
        private final TextView money;
        private final TextView company;
        private final TextView experience;
        private final TextView education;
        private final TextView address;
        private final CheckBox select;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tv_title);
            money = (TextView) itemView.findViewById(R.id.money);
            company = (TextView) itemView.findViewById(R.id.tv_company);
            experience = (TextView) itemView.findViewById(R.id.tv_experience);
            education = (TextView) itemView.findViewById(R.id.tv_education);
            address = (TextView) itemView.findViewById(R.id.tv_address);
            select = (CheckBox) itemView.findViewById(R.id.select);

        }
    }

    public void setSelectAll(boolean selectAll) {
        isSelectAll = selectAll;
    }


    private selectJob selectJob;

    public void setSelectJob(OneClickDeliveryReAdapter.selectJob selectJob) {
        this.selectJob = selectJob;
    }

    public interface selectJob {
        void selectJobId(boolean isAdd, String jobId);
    }

    private JumpToDetail jumpToDetail;

    public void setJumpToDetail(JumpToDetail jumpToDetail) {
        this.jumpToDetail = jumpToDetail;
    }

    public interface JumpToDetail {
        void turnToDetail(String JobOid);
    }

}