package com.xw.common.base
|
|
import android.annotation.SuppressLint
|
import android.content.Context
|
import android.view.LayoutInflater
|
import android.view.View
|
import android.view.ViewGroup
|
import androidx.databinding.DataBindingUtil
|
import androidx.databinding.ViewDataBinding
|
import androidx.recyclerview.widget.RecyclerView
|
import com.xw.common.R
|
import com.xw.common.databinding.CommonItemImageAddBinding
|
import com.xw.common.databinding.CommonItemImageEmptyBinding
|
import com.xw.common.databinding.CommonItemImageShowBinding
|
|
class ImageSelectAdapter(
|
var context: Context,
|
var maxSize: Int,
|
isEdit: Int = 1, // 是否可编辑 1:可编辑 else:仅查看
|
var listener: ImageEventListener<FileSelectBean>
|
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
|
var isEdit: Int = isEdit
|
@SuppressLint("NotifyDataSetChanged")
|
set(value) {
|
field = value
|
notifyDataSetChanged()
|
}
|
|
fun remove(data: FileSelectBean) {
|
val index = this.data.indexOf(data)
|
if (index == -1) {
|
return
|
}
|
this.data.removeAt(index)
|
notifyItemRemoved(index)
|
notifyItemRangeChanged(index, this.data.size - index)
|
}
|
|
@SuppressLint("NotifyDataSetChanged")
|
fun setList(list: Collection<FileSelectBean>?) {
|
if (list !== this.data) {
|
this.data.clear()
|
if (!list.isNullOrEmpty()) {
|
this.data.addAll(list)
|
}
|
} else {
|
if (!list.isEmpty()) {
|
val newList = ArrayList(list)
|
this.data.clear()
|
this.data.addAll(newList)
|
} else {
|
this.data.clear()
|
}
|
}
|
notifyDataSetChanged()
|
}
|
|
var data: MutableList<FileSelectBean> = ArrayList()
|
|
private val TYPE_ITEM = 1
|
private val TYPE_EMPTY = 2
|
private val TYPE_ADD = 3
|
|
private val inflater by lazy { LayoutInflater.from(context) }
|
|
override fun getItemCount(): Int {
|
return if (isEdit == 1) {
|
if (data.size >= maxSize) maxSize else data.size + 1
|
} else {
|
if (data.isEmpty()) 1 else data.size // 没有图片加载一个null的站位图
|
}
|
}
|
|
override fun getItemViewType(position: Int): Int {
|
return if (isEdit == 1) {
|
if (data.size == maxSize) {
|
TYPE_ITEM
|
} else {
|
if (position + 1 == itemCount) {
|
TYPE_ADD
|
} else {
|
TYPE_ITEM
|
}
|
}
|
} else {
|
if (data.isEmpty()) TYPE_EMPTY else TYPE_ITEM
|
}
|
}
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
val binding = if (viewType == TYPE_ITEM) {
|
DataBindingUtil.inflate<ViewDataBinding>(
|
inflater,
|
R.layout.common_item_image_show,
|
parent,
|
false
|
)
|
} else if (viewType == TYPE_EMPTY) {
|
DataBindingUtil.inflate<ViewDataBinding>(
|
inflater,
|
R.layout.common_item_image_empty,
|
parent,
|
false
|
)
|
} else {
|
DataBindingUtil.inflate(
|
inflater,
|
R.layout.common_item_image_add,
|
parent,
|
false
|
)
|
}
|
return ViewHolder(binding.root, binding)
|
}
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
(holder as ViewHolder).bindData(position)
|
}
|
|
inner class ViewHolder(itemView: View, var mBinding: ViewDataBinding) : RecyclerView.ViewHolder(itemView) {
|
|
fun bindData(position: Int) {
|
|
if (mBinding is CommonItemImageShowBinding) {
|
val item = data[position]
|
val binding: CommonItemImageShowBinding = mBinding as CommonItemImageShowBinding
|
binding.listener = listener
|
binding.item = item
|
binding.delete.visibility = if (isEdit == 1) View.VISIBLE else View.GONE
|
binding.ivPlay.visibility = if (item.type == "video") View.VISIBLE else View.GONE
|
} else if (mBinding is CommonItemImageAddBinding) {
|
val item = FileSelectBean()
|
val binding: CommonItemImageAddBinding = mBinding as CommonItemImageAddBinding
|
binding.listener = listener
|
binding.item = item
|
}
|
}
|
}
|
}
|
|
data class FileSelectBean(
|
var url: String = "",
|
var localPath: String = "",
|
var type: String = "", // video ic_notice audio
|
var placeholder: Int = R.mipmap.icon_default5,
|
var error: Int = R.mipmap.icon_default5,
|
){
|
override fun toString(): String {
|
return "FileSelectBean(url='$url', localPath='$localPath', type='$type')"
|
}
|
}
|