ErrorAdapter.java
8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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;
}
}
}