ErrorAdapter.java 8.11 KB
package com.hjx.parent.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hjx.parent.ErrorDetailActivity;
import com.hjx.parent.R;
import com.hjx.parent.databinding.ItemErrorBinding;
import com.prws.common.bean.ErrorDetailBean;
import com.prws.common.bean.TopicBean;
import com.prws.common.utils.CommonUtil;
import com.prws.common.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ErrorAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<TopicBean> list;
    private Context context;
    public boolean edit;
    private CheckInterface checkInterface;

    public interface CheckInterface {
        void onErrorCheck();
    }

    public ErrorAdapter(Context context, List<TopicBean> list, boolean edit, CheckInterface checkInterface) {
        this.list = list;
        this.context = context;
        this.edit = edit;
        this.checkInterface = checkInterface;
    }

    public ErrorAdapter(Context context, List<TopicBean> list, boolean edit) {
        this.list = list;
        this.context = context;
        this.edit = edit;
    }

    public void checkAll(boolean check) {
        for (TopicBean topicBean : list) {
            topicBean.setCheck(check);
        }
        notifyDataSetChanged();
        checkInterface.onErrorCheck();
    }

    public List<TopicBean> getChecKList() {
        List<TopicBean> topicBeans = new ArrayList<>();
        for (TopicBean topicBean : list) {
            if (topicBean.isCheck()) {
                topicBeans.add(topicBean);
            }
        }
        return topicBeans;
    }

    public void setEdit(boolean edit) {
        this.edit = edit;
        notifyDataSetChanged();
    }

    public void addData(List<TopicBean> topicBeans) {
        list.addAll(topicBeans);
        notifyDataSetChanged();
    }

    public void refresh(List<TopicBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemErrorBinding itemTopicBinding = ItemErrorBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ErrorHolder(itemTopicBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        TopicBean topicBean = list.get(position);
        ErrorHolder holder = (ErrorHolder) viewHolder;
        holder.binding.ivCheck.setVisibility(edit ? View.VISIBLE : View.GONE);
        holder.binding.ivCheck.setImageResource(topicBean.isCheck() ? R.mipmap.ic_select : R.mipmap.ic_unselect);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (position == 0 || !format.format(list.get(position - 1).getTime()).equals(format.format(topicBean.getTime()))) {
            holder.binding.tvTime.setVisibility(View.VISIBLE);
            holder.binding.tvTime.setText(format.format(topicBean.getTime()));
        } else {
            holder.binding.tvTime.setVisibility(View.GONE);
        }
        if (topicBean.isDone()) {
            holder.binding.tvType.setText(context.getString(R.string.text_done));
            holder.binding.tvType.setBackgroundResource(R.drawable.bg_done_text);
        } else {
            holder.binding.tvType.setText(context.getString(R.string.text_undo));
            holder.binding.tvType.setBackgroundResource(R.drawable.bg_undo_text);
        }
        holder.binding.tvReason.setText("错因:" + context.getResources().getStringArray(R.array.error_reason)[topicBean.getReason()]);
        if (topicBean.getBean() != null) {
            ErrorDetailBean bean = topicBean.getBean();
            holder.binding.ivTopic.setVisibility(View.GONE);
            String linkCss = "<link rel=\"stylesheet\" href=\"file:///android_asset/style.css\" type=\"text/css\">";
            StringBuilder sb = new StringBuilder(4096);
            if (!TextUtils.isEmpty(bean.getLabel())) {
                sb.append(bean.getLabel());
            }
            sb.append(bean.getContent());
            if (bean.getOptions() != null && bean.getOptions().size() > 0) {
                sb.append("<div class='pt1'>");
                sb.append(StringUtils.OptionsHtml(bean.getOptions(), null));
                sb.append("</div>");
            }
            String body = "<html><header>" + linkCss + "</header>" + sb.toString() + "</body></html>";
            WebSettings settings = holder.binding.webview.getSettings();
//            settings.setUseWideViewPort(true);//设定支持 viewport
//            settings.setLoadWithOverviewMode(true);   //自适应屏幕
//            settings.setBuiltInZoomControls(true);
//            settings.setDisplayZoomControls(false);
//            settings.setSupportZoom(false);//设定支持缩放
            holder.binding.webview.loadDataWithBaseURL(linkCss, body, "text/html", "UTF-8", null);
            holder.binding.webview.setVisibility(View.VISIBLE);
            if (bean.getPoints().size() > 0) {
                String topic = context.getString(R.string.topic_start);
                topic += ":";

                for (int j = 0; j < bean.getPoints().size(); j++) {
                    if (j > 0) {
                        topic += "、";
                    }
                    topic += bean.getPoints().get(j).getValue();
                }
                holder.binding.tvPoint.setText(topic);
                holder.binding.tvPoint.setVisibility(View.VISIBLE);
            } else {
                holder.binding.tvPoint.setVisibility(View.GONE);
            }
        } else {
            holder.binding.webview.setVisibility(View.GONE);
            holder.binding.tvPoint.setVisibility(View.GONE);
            holder.binding.ivTopic.setVisibility(View.VISIBLE);
            int maxWidth = CommonUtil.getScreenWidth(context) - CommonUtil.dpToPx(context, 60);
            Glide.with(context).load(topicBean.getPath()).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    int width = resource.getIntrinsicWidth();
                    int height = resource.getIntrinsicHeight();
                    int newHeight = maxWidth * height / width;
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.binding.ivTopic.getLayoutParams();
                    layoutParams.width = maxWidth;
                    layoutParams.height = newHeight;
                    holder.binding.ivTopic.setLayoutParams(layoutParams);
                    holder.binding.ivTopic.setImageDrawable(resource);
                }
            });
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit) {
                    list.get(position).setCheck(!topicBean.isCheck());
                    notifyItemChanged(position);
                    checkInterface.onErrorCheck();
                } else {
                    Intent intent = new Intent(context, ErrorDetailActivity.class);
                    intent.putExtra("errorBean", topicBean);
                    context.startActivity(intent);
                }
            }
        });
    }

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

    static class ErrorHolder extends RecyclerView.ViewHolder {
        ItemErrorBinding binding;

        public ErrorHolder(@NonNull ItemErrorBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }
}