CymChad / CymChad/BaseRecyclerViewAdapterHelper

3.0.4不响应点击事件

Open
#3,489 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
24.6k
Forks
5.2k
PR merge metrics
No merged PRs in 30d

Description

class ItemAdapter : BaseQuickAdapter<ToDo, BaseViewHolder> {

    val TAG = javaClass.simpleName

    constructor(data: MutableList<ToDo>?) : super(0, data){
        addChildClickViewIds(R.id.item_select, R.id.item_content)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val itemLayout = ItemLayout(context!!)
        return createBaseViewHolder(itemLayout)
    }

    override fun convert(holder: BaseViewHolder, item: ToDo) {
//        Log.e("ItemAdapter", "convert item =$item")
        val rootView = holder.itemView as ItemLayout
        rootView.select.isChecked = item.isFinish
        if (item.isFinish) {
            //文本显示删除线效果
            rootView.content.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG)
        } else {
            //取消删除线
            val textView = rootView.content
            textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG)
            textView.paintFlags = textView.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        }
        (holder.itemView as ItemLayout).content.text = item.content
    }
}

adapter = ItemAdapter(todoList)
        mLayout!!.recyclerView.adapter = adapter
        adapter?.setOnItemClickListener { adapter, view, position ->
            Log.e(TAG, "OnItemClickListener position = $position ")
        }
        adapter?.setOnItemChildClickListener { adapter, view, position ->
            Log.e(TAG, "position = $position ")
            if (view is CheckBox) {
                Log.e(TAG, "position = $position  isChecked = $view.isChecked")
                todoList[position].isFinish = view.isChecked
            }
        }



class ItemLayout(context: Context) : CustomLayout(context) {

    init {
        layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 50.dp)
        isClickable = true
    }

    val select: CheckBox = CheckBox(context).apply {
        id = R.id.item_select
        View.generateViewId()
//        Log.e(TAG, "select id :$id ")
        //配置列表宽高
        layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
        //将控件添加到当前自定义的ViewGroup
        addView(this)
    }

    val content: TextView = TextView(context).apply {
        id = R.id.item_content
        isClickable = true
        textSize = 20f
        //配置列表宽高
        layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
        //将控件添加到当前自定义的ViewGroup
        addView(this)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        select.autoMeasure()
        content.autoMeasure()
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        //居中算法:(父height - 子height)/2
        val selectTop = (this.measuredHeight - select.measuredHeight) / 2
        select.let { it.layout(0, selectTop, it.measuredWidth, selectTop + it.measuredHeight) }
        val contentTop = (this.measuredHeight - content.measuredHeight) / 2
        content.let {
            it.layout(
                select.width + select.marginLeft + select.marginRight + select.paddingLeft + select.paddingRight,
                contentTop,
                it.measuredWidth + select.width + select.marginLeft + select.marginRight + select.paddingLeft + select.paddingRight,
                contentTop + it.measuredHeight
            )
        }
    }

}

点击事件里面的log完全不打印

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ItemAdapter click-listener registration and the ItemLayout children, especially the clickable CheckBox and TextView. Reproduce the issue and inspect whether clicks reach the adapter callbacks; done means the relevant Log.e messages appear for item and child clicks.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.