97 lines
4.0 KiB
Kotlin
97 lines
4.0 KiB
Kotlin
package com.gh.gamecenter.toolbox
|
|
|
|
import android.content.Context
|
|
import android.view.ViewGroup
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
import com.gh.base.BaseRecyclerViewHolder
|
|
import com.gh.common.util.goneIf
|
|
import com.gh.common.util.safelyGetInRelease
|
|
import com.gh.gamecenter.baselist.ListExecutor
|
|
import com.gh.gamecenter.databinding.ToolboxBlockItemBinding
|
|
import com.gh.gamecenter.entity.ToolBoxBlockEntity
|
|
import com.lightgame.adapter.BaseRecyclerAdapter
|
|
import java.util.*
|
|
|
|
class ToolBoxBlockAdapter(context: Context) : BaseRecyclerAdapter<ToolBoxBlockAdapter.ToolBoxBlockViewHolder>(context) {
|
|
|
|
private var mEntityList = listOf<ToolBoxBlockEntity>()
|
|
|
|
fun setDataList(dataList: List<ToolBoxBlockEntity>) {
|
|
if (dataList.isEmpty() || mEntityList.size > dataList.size) {
|
|
mEntityList = dataList
|
|
notifyDataSetChanged()
|
|
return
|
|
}
|
|
ListExecutor.workerExecutor.execute {
|
|
val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
|
|
override fun getOldListSize(): Int {
|
|
return mEntityList.size
|
|
}
|
|
|
|
override fun getNewListSize(): Int {
|
|
return dataList.size
|
|
}
|
|
|
|
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
|
|
val oldItem = mEntityList.safelyGetInRelease(oldItemPosition)
|
|
val newItem = dataList.safelyGetInRelease(newItemPosition)
|
|
return oldItem == newItem
|
|
}
|
|
|
|
override fun areContentsTheSame(
|
|
oldItemPosition: Int,
|
|
newItemPosition: Int
|
|
): Boolean {
|
|
val oldItem = mEntityList.safelyGetInRelease(oldItemPosition)
|
|
val newItem = dataList.safelyGetInRelease(newItemPosition)
|
|
return oldItem == newItem
|
|
}
|
|
})
|
|
ListExecutor.uiExecutor.execute {
|
|
mEntityList = ArrayList(dataList)
|
|
diffResult.dispatchUpdatesTo(this)
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: ToolBoxBlockViewHolder, position: Int) {
|
|
holder.bindToolBoxBlock(mEntityList[position])
|
|
holder.binding.divider.goneIf(position == itemCount - 1 || mEntityList[position].categoryName == "最近使用")
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
|
|
ToolBoxBlockViewHolder(ToolboxBlockItemBinding.inflate(mLayoutInflater, parent, false))
|
|
|
|
override fun getItemCount() = mEntityList.size
|
|
|
|
class ToolBoxBlockViewHolder(val binding: ToolboxBlockItemBinding) :
|
|
BaseRecyclerViewHolder<ToolBoxBlockEntity>(binding.root) {
|
|
|
|
private var mIsExpand = false
|
|
private var mAdapter: ToolBoxItemAdapter? = null
|
|
|
|
fun bindToolBoxBlock(toolBoxBlockEntity: ToolBoxBlockEntity) {
|
|
val context = binding.root.context
|
|
mAdapter = ToolBoxItemAdapter(context, true)
|
|
mAdapter?.setDataList(toolBoxBlockEntity.toolboxList.take(4))
|
|
binding.titleTv.text = toolBoxBlockEntity.categoryName
|
|
binding.toolboxRv.layoutManager = GridLayoutManager(context, 2)
|
|
binding.toolboxRv.adapter = mAdapter
|
|
binding.expandContainer.goneIf(toolBoxBlockEntity.toolboxList.size < 5)
|
|
binding.expandContainer.setOnClickListener {
|
|
mIsExpand = !mIsExpand
|
|
if (mIsExpand)
|
|
mAdapter?.setDataList(toolBoxBlockEntity.toolboxList)
|
|
else
|
|
mAdapter?.setDataList(toolBoxBlockEntity.toolboxList.take(4))
|
|
binding.expandTv.text = if (mIsExpand) "收起" else "展开全部"
|
|
binding.expandIv.run {
|
|
pivotX = width / 2F
|
|
pivotY = height / 2F
|
|
rotation = if (mIsExpand) 180F else 0F
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |