package com.example.smart.ui.item.adapter import android.app.Activity import android.content.Context import android.text.TextUtils import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView.Adapter import androidx.recyclerview.widget.RecyclerView.ViewHolder import com.bumptech.glide.Glide import com.example.basecore.util.startAct import com.example.smart.R import com.example.smart.bean.ImageDrawData import com.example.smart.bean.ImageRef import com.example.smart.oss.ImageRemotePathProvider import com.example.smart.ui.icondraw.DrawActivity import com.example.smart.ui.item.bean.ImageBean import com.example.smart.util.openImageViewerScheme class ImageShowAdapter( private val mContext: Context, val data: ArrayList, val needDraw: Boolean = true, val showClear:Boolean = true ) : Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder { return PageViewHolder( LayoutInflater.from(mContext).inflate(R.layout.item_add_image, parent, false) ) } override fun getItemCount() = data.size override fun onBindViewHolder(holder: PageViewHolder, position: Int) { holder.takeLayout.setOnClickListener {} val bean = data[position] holder.takeLayout.isVisible = false holder.pic_layout.isVisible = true val path = ImageRemotePathProvider.IMAGE_BASE_URL + bean.accessPath if (!TextUtils.isEmpty(bean.accessPath)) { Glide.with(mContext).load(path).into(holder.pic_img) } else { holder.takeLayout.isVisible = true holder.pic_layout.isVisible = false } if (needDraw) { holder.editView.visibility = View.VISIBLE } else { holder.editView.visibility = View.GONE } holder.editView.setOnClickListener { if (TextUtils.isEmpty(path)) { return@setOnClickListener } startAct(mContext) { putExtra("id", bean.uri) putExtra("path", path) } } holder.pic_img.setOnClickListener { val paths = ArrayList() for (i in data) { paths.add(ImageRemotePathProvider.IMAGE_BASE_URL + i.accessPath) } openImageViewerScheme(mContext as Activity, paths, position) } if (showClear) { holder.clear_img.visibility = View.VISIBLE } else{ holder.clear_img.visibility = View.GONE } holder.clear_img.setOnClickListener { data.remove(bean) notifyDataSetChanged() } } fun updateData(dataList: List){ data.clear() dataList.forEach { data.add(it) } notifyDataSetChanged() } fun notifyData(dataList: List) { dataList.forEach { data.add(ImageBean(it.id, it.name)) } notifyDataSetChanged() } fun notifyData() { notifyDataSetChanged() } /** * 更新绘制过的数据 */ fun updateDrawData(bean: ImageDrawData) { data.forEach { if (it.uri == bean.oldId) { it.accessPath = bean.name it.uri = bean.id notifyItemChanged(data.indexOf(it)) } } } fun clearAndAddData(dataList: List) { data.clear() data.addAll(dataList) notifyDataSetChanged() } fun addImage(bean: ImageBean) { data.add(bean) // notifyDataSetChanged() notifyItemInserted(data.size - 1) } fun updateImageBean(path: String) { if (data.size == 0) { return } val bean = data[data.size - 1] bean.accessPath = path } fun notifyItemShowImg(bean: ImageBean) { val index = data.indexOf(bean) if (index >= 0) { notifyItemChanged(index) } } fun deleteImage() { val index = data.size - 1 data.remove(data[index]) notifyItemRemoved(index) } fun deleteImage(position: Int) { val bean = data[position] data.remove(bean) notifyItemRemoved(position) } inner class PageViewHolder(itemView: View) : ViewHolder(itemView) { val takeLayout: View val pic_layout: View val pic_img: ImageView val clear_img: ImageView val editView: View init { takeLayout = itemView.findViewById(R.id.take_photo_layout) pic_layout = itemView.findViewById(R.id.pic_layout) pic_img = itemView.findViewById(R.id.pic_img) clear_img = itemView.findViewById(R.id.clear_img) editView = itemView.findViewById(R.id.draw_img) } } }