欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题

程序员文章站 2022-06-22 21:55:37
解决RecycleView 中Item包含Edittext时,滑动导致数据错乱的问题一言不合就上代码:override fun onBindViewHolder(p0: InnerHolder, position: Int) { var item :AppOnShelfDetail = mStringSparseArray[position] p0.tv_content_code.text = item.itemCode //商品编码 p0.tv_r...

解决RecycleView 中Item包含Edittext时,滑动导致数据错乱的问题

一言不合就上代码:

override fun onBindViewHolder(p0: InnerHolder, position: Int) {
        var item :AppOnShelfDetail = mStringSparseArray[position]
        p0.tv_content_code.text = item.itemCode //商品编码
        p0.tv_ready_up_number.text = StringUtil.intToString(item.reqQty)
        if (p0.editText.tag is TextWatcher) {
            p0.editText.removeTextChangedListener(p0.editText.tag as TextWatcher?)
        }

        if (p0.editTextCode.tag is TextWatcher) {
            p0.editTextCode.removeTextChangedListener(p0.editTextCode.tag as TextWatcher?)
        }

        val editWatcher = EditWatcher(position, item,p0)
        val editWatcherCode = EditWatcherCode(position,item, p0)

        p0.editText.addTextChangedListener(editWatcher)
        p0.editTextCode.addTextChangedListener(editWatcherCode)

        p0.editText.tag = editWatcher
        p0.editTextCode.tag = editWatcher
        if (mStringSparseArray[position].onshelfQty == 0)  p0.editText.setText("")
        else p0.editText.setText(StringUtil.intToString(mStringSparseArray[position].onshelfQty))//实际上架数量
        p0.editTextCode.setText(mStringSparseArray[position].locationCode)//上架库位

    }

    inner class EditWatcher(val position: Int,var item :AppOnShelfDetail, val hoilder: GoodsUpDetailAdapter.InnerHolder) : TextWatcher {

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun afterTextChanged(content: Editable?) {

            if (!content.isNullOrEmpty()) {
                val readyUpCount: Int =  hoilder.tv_ready_up_number.text.toString().toInt()
                    val signCount: Int = content.toString().toInt()//实上架数量
                    if (signCount <= readyUpCount) {
                        item.onshelfQty = signCount
                    }else{
                        ToastUtils.show("实上架数量大于待上架数量,请重新输入")
                    }

                mStringSparseArray.put(position, item)
            }

        }


        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

    }


 inner class EditWatcherCode(val position: Int,var item :AppOnShelfDetail, val hoilder: GoodsUpDetailAdapter.InnerHolder) : TextWatcher {

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun afterTextChanged(content: Editable?) {

            if (!content.isNullOrEmpty()) {
                var locationCode: String = content.toString()//唯一码
                item.locationCode = locationCode
                mStringSparseArray.put(position, item)
            }
//            else {
//                mStringSparseArray.delete(position)
//            }
        }


        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

    }

核心代码就这么多,接下来再加上 局部刷新,就可以解决滑动复用导致的数据错乱问题

   fun setData(stringSparseArray: SparseArray<Object>, count: Int,index:Int) {
        mStringSparseArray = stringSparseArray
        mCount = count
//        notifyDataSetChanged()
        notifyItemChanged(index)
    }

 

本文地址:https://blog.csdn.net/u013075460/article/details/107635170