diff --git a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/badcase/biz/adapter/TopicListAdapter.kt b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/badcase/biz/adapter/TopicListAdapter.kt index 7d1c810990..dc7ffabf0b 100644 --- a/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/badcase/biz/adapter/TopicListAdapter.kt +++ b/core/function-impl/mogo-core-function-devatools/src/main/java/com/zhjt/mogo_core_function_devatools/badcase/biz/adapter/TopicListAdapter.kt @@ -15,10 +15,10 @@ import com.zhjt.mogo_core_function_devatools.R */ class TopicListAdapter: RecyclerView.Adapter() { - private var data:List? = null + private var data:MutableList? = null private var topicClickListener: TopicClickListener? = null - fun setData( data: List?){ + fun setData( data: MutableList?){ this.data = data } @@ -39,13 +39,37 @@ class TopicListAdapter: RecyclerView.Adapter() holder.topic_check_box.isChecked = topicList[position].topicStatus holder.topic_check_box.tag = topicList holder.topic_check_box.isClickable = topicList[position].topicCanClick - holder.topic_check_box.setOnCheckedChangeListener { _, b -> - topicList[position].topicStatus = b - topicClickListener?.onClick(topicList[position].topicName,b) + holder.topic_check_box.setOnCheckedChangeListener { _, isChecked -> + topicList[position].topicStatus = isChecked + topicClickListener?.onClick(topicList[position].topicName,isChecked) + if(isChecked){ + //滚动到置顶 + moveItem(topicList[position],position,0) + }else{ + var lastNotCan = 0 //最后一个不能选择的 + for( i in 0 until itemCount){ + var topicEntity = data?.get(i) + if (topicEntity != null) { + if(!topicEntity.topicCanClick){ + lastNotCan = i + } + } + } + moveItem(topicList[position],position,lastNotCan) + } } } } + private fun moveItem(topicEntity: TopicEntity,removePos: Int,insertedPos: Int){ + data?.remove(topicEntity) + notifyItemRemoved(removePos) + notifyItemRangeChanged(removePos, itemCount - removePos) + data?.add(insertedPos, topicEntity) + notifyItemInserted(insertedPos) + notifyItemRangeChanged(insertedPos, itemCount) + } + override fun getItemCount() = data?.size ?: 0 class TopicListHolder(itemView: View) : RecyclerView.ViewHolder(itemView){