|
|
package com.br_technology.securitytrain_master.ui.view.bank.adapter
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
import android.app.Activity
|
|
|
import android.content.Context
|
|
|
import android.graphics.Color
|
|
|
import android.text.SpannableString
|
|
|
import android.text.Spanned
|
|
|
import android.text.style.AbsoluteSizeSpan
|
|
|
import android.text.style.ForegroundColorSpan
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
import android.widget.TextView
|
|
|
import androidx.core.content.ContextCompat
|
|
|
import com.br_technology.securitytrain_master.R
|
|
|
import com.br_technology.securitytrain_master.base.common.ConstantType
|
|
|
import com.br_technology.securitytrain_master.base.view.BaseAdapter
|
|
|
import com.br_technology.securitytrain_master.base.view.BaseViewHolder
|
|
|
import com.br_technology.securitytrain_master.databinding.AdapterContentPracticeItemBinding
|
|
|
import com.br_technology.securitytrain_master.databinding.AdapterTopicBinding
|
|
|
import com.br_technology.securitytrain_master.expand.dp2px
|
|
|
import com.br_technology.securitytrain_master.ui.bean.Answer
|
|
|
import com.br_technology.securitytrain_master.ui.bean.AnswerDetail
|
|
|
import com.br_technology.securitytrain_master.ui.bean.AnswerRecord
|
|
|
import com.br_technology.securitytrain_master.ui.view.bank.bean.OptionArr
|
|
|
import com.br_technology.securitytrain_master.ui.view.bank.bean.TestSubject
|
|
|
import com.br_technology.securitytrain_master.util.GlideEnginePic
|
|
|
import com.br_technology.securitytrain_master.view.listener.OnItemClickListener
|
|
|
import com.br_technology.securitytrain_master.view.listener.OnItemListener
|
|
|
import com.luck.picture.lib.PictureSelector
|
|
|
import com.luck.picture.lib.entity.LocalMedia
|
|
|
|
|
|
/**
|
|
|
* auth: 张继
|
|
|
* date: 2022/1/24 16:21
|
|
|
* dsc:
|
|
|
* updateInfo:
|
|
|
*/
|
|
|
|
|
|
|
|
|
class AnswerRecordDetailsItemAdapter : BaseAdapter<OptionArr, AdapterTopicBinding>() {
|
|
|
// 题目类型 1:单选 2:多选 3:判断
|
|
|
var type = 1
|
|
|
|
|
|
// 是否显示正确答案
|
|
|
var showCorrect = false
|
|
|
|
|
|
// 选中选项
|
|
|
var mSelectIndex = mutableListOf<Int>()
|
|
|
|
|
|
var mListener: OnItemListener? = null
|
|
|
|
|
|
override fun getViewBinding(
|
|
|
context: Context,
|
|
|
parent: ViewGroup,
|
|
|
viewType: Int,
|
|
|
from: LayoutInflater
|
|
|
): AdapterTopicBinding {
|
|
|
return AdapterTopicBinding.inflate(from, parent, false)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 选中处理
|
|
|
*/
|
|
|
fun itemClick(position: Int) {
|
|
|
if (!mSelectIndex.contains(position)) {
|
|
|
mSelectIndex.add(position)
|
|
|
} else {
|
|
|
mSelectIndex.remove(position)
|
|
|
}
|
|
|
notifyDataSetChanged()
|
|
|
}
|
|
|
|
|
|
fun setItemClickListener(listener: OnItemListener) {
|
|
|
this.mListener = listener
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 是否为选中
|
|
|
*/
|
|
|
fun isSelected(position: Int): Boolean {
|
|
|
return mSelectIndex.contains(position)
|
|
|
}
|
|
|
|
|
|
override fun onBindViewHolder(holder: BaseViewHolder<AdapterTopicBinding>, position: Int) {
|
|
|
super.onBindViewHolder(holder, position)
|
|
|
holder.itemView.isEnabled = !showCorrect
|
|
|
holder.itemView.setOnClickListener {
|
|
|
mListener?.onItemClick(position)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
override fun onBind(holder: AdapterTopicBinding, position: Int, data: OptionArr) {
|
|
|
holder.tvName.text = "${data.option_name}.${data.value}"
|
|
|
|
|
|
if (data.pic_arr.isNotEmpty()) {
|
|
|
val pictureAdapter = PictureAdapter(holder.root.context)
|
|
|
pictureAdapter.addList(data.pic_arr.toMutableList())
|
|
|
holder.ivPic.adapter = pictureAdapter
|
|
|
holder.ivPic.setOnTouchListener { v, event ->
|
|
|
holder.contentTopic.onTouchEvent(event)
|
|
|
false
|
|
|
}
|
|
|
pictureAdapter.addListener(object : OnItemClickListener<String> {
|
|
|
override fun onClick(position: Int, pic: String) {
|
|
|
previewPhoto(data.pic_arr, holder.root.context, position)
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
setTextDefault(holder.contentTopic, holder.tvName)
|
|
|
// 单选 / 判断
|
|
|
if (type == 1 || type == 3) {
|
|
|
if (mSelectIndex.isNotEmpty() && mSelectIndex[0] == position) {
|
|
|
if (showCorrect) {
|
|
|
if (data.is_correct == 1) {
|
|
|
setTextGreen(holder.contentTopic, holder.tvName)
|
|
|
} else {
|
|
|
setTextRed(holder.contentTopic, holder.tvName)
|
|
|
}
|
|
|
} else {
|
|
|
//未交卷时显示
|
|
|
setTextGreen(holder.contentTopic, holder.tvName)
|
|
|
}
|
|
|
} else {
|
|
|
if (showCorrect && data.is_correct == 1) {
|
|
|
// 正确答背景应变绿色--需求修改成当有错题时正确答案背景色没变化
|
|
|
// setTextGreen(holder.contentTopic, holder.tvName)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 多选
|
|
|
if (type == 2) {
|
|
|
if (isSelected(position)) {
|
|
|
if (showCorrect) {
|
|
|
if (data.is_correct == 1) {
|
|
|
setTextGreen(holder.contentTopic, holder.tvName)
|
|
|
} else {
|
|
|
setTextRed(holder.contentTopic, holder.tvName)
|
|
|
}
|
|
|
} else {
|
|
|
setTextGreen(holder.contentTopic, holder.tvName)
|
|
|
}
|
|
|
} else {
|
|
|
if (showCorrect && data.is_correct == 1) {
|
|
|
// 正确答背景应变绿色--需求修改成当有错题时正确答案背景色没变化
|
|
|
// setTextGreen(holder.contentTopic, holder.tvName)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private fun setTextDefault(view: View, textView: TextView) {
|
|
|
view.setBackgroundResource(R.drawable.stroke_eb_4)
|
|
|
textView.setTextColor(ContextCompat.getColor(textView.context, R.color.color_32))
|
|
|
}
|
|
|
|
|
|
private fun setTextRed(view: View, textView: TextView) {
|
|
|
view.setBackgroundResource(R.drawable.topic_back_error)
|
|
|
textView.setTextColor(ContextCompat.getColor(textView.context, R.color.color_e9))
|
|
|
}
|
|
|
|
|
|
private fun setTextGreen(view: View, textView: TextView) {
|
|
|
view.setBackgroundResource(R.drawable.solid_0825_4)
|
|
|
textView.setTextColor(ContextCompat.getColor(textView.context, R.color.color_25))
|
|
|
}
|
|
|
|
|
|
private fun previewPhoto(data: Array<String>, context: Context, position: Int) {
|
|
|
val list = mutableListOf<LocalMedia>()
|
|
|
data.forEach {
|
|
|
val localMedia = LocalMedia()
|
|
|
localMedia.path = it
|
|
|
list.add(localMedia)
|
|
|
}
|
|
|
PictureSelector.create(context as Activity)
|
|
|
.themeStyle(R.style.picture_default_style)
|
|
|
.imageEngine(GlideEnginePic.createGlideEngine())
|
|
|
.openExternalPreview(position, list)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
class AnswerRecordDetailsAdapter(private val isShowPosition: Boolean = true) :
|
|
|
BaseAdapter<AnswerDetail, AdapterContentPracticeItemBinding>() {
|
|
|
// 是否显示正确答案
|
|
|
var showCorrect = false
|
|
|
var selectResultData = HashMap<Int, List<Int>>()
|
|
|
|
|
|
override fun getViewBinding(
|
|
|
context: Context,
|
|
|
parent: ViewGroup,
|
|
|
viewType: Int,
|
|
|
from: LayoutInflater
|
|
|
): AdapterContentPracticeItemBinding {
|
|
|
return AdapterContentPracticeItemBinding.inflate(from, parent, false)
|
|
|
}
|
|
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
override fun onBind(
|
|
|
holder: AdapterContentPracticeItemBinding,
|
|
|
index: Int,
|
|
|
data: AnswerDetail
|
|
|
) {
|
|
|
|
|
|
val value = if (isShowPosition) {
|
|
|
"(${index + 1}/${itemCount})${data.item.title}${ConstantType.getType(data.type)}"
|
|
|
} else {
|
|
|
"${data.item.title}${ConstantType.getType(data.type)}"
|
|
|
}
|
|
|
val span = SpannableString(value)
|
|
|
|
|
|
span.setSpan(
|
|
|
ForegroundColorSpan(Color.parseColor("#C8C9CC")),
|
|
|
value.length - 4,
|
|
|
value.length,
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
|
)
|
|
|
span.setSpan(
|
|
|
AbsoluteSizeSpan(14.dp2px()),
|
|
|
value.length - 4,
|
|
|
value.length,
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
|
)
|
|
|
|
|
|
holder.topic.text = span
|
|
|
val adapterChild = AnswerRecordDetailsItemAdapter()
|
|
|
adapterChild.addList(data.item.options_arr)
|
|
|
holder.topicRecycler.adapter = adapterChild
|
|
|
holder.txtCorrect.text = "正确答案:${data.item.correct}"
|
|
|
holder.txtAnalysis.text = "解析:${data.item.remark}"
|
|
|
holder.contentAnalysis.visibility = if (showCorrect) View.VISIBLE else View.GONE
|
|
|
adapterChild.let {
|
|
|
val type = data.type.toInt()
|
|
|
it.type = type
|
|
|
it.showCorrect = showCorrect
|
|
|
|
|
|
if (type == 1 || type == 3) {
|
|
|
for (position in data.item.options_arr.indices) {
|
|
|
if (data.item.options_arr[position].option_name == data.answer) {
|
|
|
it.itemClick(position)
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
for (position in data.item.options_arr.indices) {
|
|
|
val split = data.answer.split(",")
|
|
|
for (j in split.indices) {
|
|
|
if (data.item.options_arr[position].option_name == split[j]) {
|
|
|
it.itemClick(index)
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (selectResultData.containsKey(index)) {
|
|
|
selectResultData[index]?.let { data ->
|
|
|
if (data.isNotEmpty()) {
|
|
|
it.mSelectIndex = data as MutableList<Int>
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
it.setItemClickListener(object : OnItemListener {
|
|
|
override fun onItemClick(position: Int) {
|
|
|
if (type == 1 || type == 3) {
|
|
|
if (it.mSelectIndex.isEmpty()) {
|
|
|
it.mSelectIndex.add(position)
|
|
|
} else {
|
|
|
it.mSelectIndex[0] = position
|
|
|
}
|
|
|
it.notifyDataSetChanged()
|
|
|
} else {
|
|
|
it.itemClick(position)
|
|
|
}
|
|
|
selectResultData[index] = it.mSelectIndex
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
override fun addList(t: List<AnswerDetail>) {
|
|
|
super.addList(t)
|
|
|
for (data in getData()) {
|
|
|
val index = getData().indexOf(data)
|
|
|
selectResultData[index] = listOf()
|
|
|
val selectIndex = mutableListOf<Int>()
|
|
|
|
|
|
if (data.type == "1" || data.type == "3") {
|
|
|
for (position in data.item.options_arr.indices) {
|
|
|
if (data.item.options_arr[position].option_name == data.answer) {
|
|
|
selectIndex.add(position)
|
|
|
selectResultData[index] = selectIndex
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
for (position in data.item.options_arr.indices) {
|
|
|
val split = data.answer.split(",")
|
|
|
for (j in split.indices) {
|
|
|
if (data.item.options_arr[position].option_name == split[j]) {
|
|
|
selectIndex.add(position)
|
|
|
selectResultData[index] = selectIndex
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private fun getResultData(): ArrayList<List<Int>> {
|
|
|
val data = ArrayList<List<Int>>()
|
|
|
val result = selectResultData.keys
|
|
|
val dataKey = mutableListOf<Int>()
|
|
|
for (item in result) {
|
|
|
dataKey.add(item)
|
|
|
}
|
|
|
dataKey.sort()
|
|
|
for (index in dataKey) {
|
|
|
selectResultData[index]?.let {
|
|
|
data.add(it)
|
|
|
}
|
|
|
}
|
|
|
return data
|
|
|
}
|
|
|
|
|
|
fun getAnswer(): ArrayList<Answer> {
|
|
|
val data = ArrayList<Answer>()
|
|
|
val list = getResultData()
|
|
|
val listSubject = getData()
|
|
|
for (res in list.indices) {
|
|
|
val pos = list[res]
|
|
|
val ppp = listSubject[res]
|
|
|
data.add(Answer(ppp.item_id, getAnswer(ppp.item.options_arr, pos)))
|
|
|
}
|
|
|
return data
|
|
|
}
|
|
|
|
|
|
fun getAnswer(optionArr: List<OptionArr>, data: List<Int>): String {
|
|
|
var str = ""
|
|
|
if (data.isNotEmpty()) {
|
|
|
for (res in data) {
|
|
|
if (res < optionArr.size) {
|
|
|
str = str + optionArr[res].option_name + ","
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return str.substringBeforeLast(",");
|
|
|
}
|
|
|
|
|
|
fun getCount(): Int {
|
|
|
var count = 0
|
|
|
for (data in selectResultData) {
|
|
|
if (data.value.isNotEmpty()) {
|
|
|
count++
|
|
|
}
|
|
|
}
|
|
|
return count
|
|
|
}
|
|
|
|
|
|
fun getArrayList(): ArrayList<String> {
|
|
|
val list = ArrayList<String>()
|
|
|
val dataList = getResultData()
|
|
|
for (data in getData()) {
|
|
|
val position = getData().indexOf(data)
|
|
|
if (dataList[position].isEmpty()) {
|
|
|
list.add("")
|
|
|
} else {
|
|
|
val element = if (isTrue(data, dataList[position], data.item.options_arr)
|
|
|
) "1" else "0"
|
|
|
list.add(element)
|
|
|
}
|
|
|
}
|
|
|
return list
|
|
|
}
|
|
|
|
|
|
private fun isTrue(
|
|
|
testSubject: AnswerDetail,
|
|
|
mSelectIndex: List<Int>,
|
|
|
optionArr: List<OptionArr>
|
|
|
): Boolean {
|
|
|
var bool = false
|
|
|
val type = testSubject.type.toInt()
|
|
|
if (type == 1 || type == 3) {
|
|
|
bool = if (mSelectIndex.isNotEmpty()) {
|
|
|
optionArr[mSelectIndex[0]].is_correct == 1
|
|
|
} else {
|
|
|
false
|
|
|
}
|
|
|
}
|
|
|
if (type == 2) {
|
|
|
var count = 0
|
|
|
var dataCount = 0
|
|
|
for (item in optionArr) {
|
|
|
val position = optionArr.indexOf(item)
|
|
|
if (item.is_correct == 1) {
|
|
|
if (mSelectIndex.contains(position)) {
|
|
|
count++
|
|
|
}
|
|
|
dataCount++;
|
|
|
}
|
|
|
}
|
|
|
bool = count == dataCount && mSelectIndex.size == count
|
|
|
}
|
|
|
return bool
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
...
|
...
|
|