RecycLerView item中含有Edittext情况处理

近期开发需求:住宅收录,每一楼层户室上限500,可增删改

实现思路1、使用RecyclerView。

2、在适配器添加TextWatcher实现对Edittext内容变化的监听,这其中涉及到一个问题,由于我们的需求上限是500条,如果每个都要添加会造成卡顿甚至崩溃,所以我们可以利用RecyclerView中的复用机制来实现我们的目的。

代码中关于索引的+1,-1,的问题是由于楼层名称那块用的是头布局,占用了索引0呢。

实现代码

adapter


class ResidenceFamilyRecyclerAdapter(layourId : Int ,data : List<LayerOneBean.RoomsBean>) : BaseQuickAdapter<LayerOneBean.RoomsBean,BaseViewHolder>(layourId,data){
    private var layerName = ""
    private var position = 1
    private lateinit var textWatcher : TextWatcher
    private lateinit var etContent:MediumBoldEditText
    private var flag = true

    fun setLayerName(name : String){
        layerName = name
    }
    override fun convert(helper: BaseViewHolder?, item: LayerOneBean.RoomsBean?) {
        position = helper!!.adapterPosition - 1
        helper!!.setText(R.id.tv_content,"户室"+(helper.adapterPosition))


        etContent = helper.getView<MediumBoldEditText>(R.id.et_content)
        if (!TextUtils.isEmpty(item?.roomName)){
            etContent.setText(item?.roomName)
            etContent.editTextWidth = 1F
        }else{
            etContent.setText(item?.roomName)
            etContent.editTextWidth = 0F
        }

        etContent.setHint("填写户室号,默认"+layerName+String.format("%02d", helper.adapterPosition))

        if (etContent.getTag() is TextWatcher) {
            etContent.removeTextChangedListener(etContent.getTag() as TextWatcher)
        }
        if (1 != helper!!.adapterPosition){

            helper.setBackgroundColor(R.id.rl_bg,mContext.resources.getColor(R.color.color_f9f9f9))
        }else{
            helper.setBackgroundRes(R.id.rl_bg,R.drawable.shape_rect_f9f9f9_top_8)
        }

        helper.addOnClickListener(R.id.iv_delete)

        textWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {

                if (flag) {
                    flag = false
                    if ( this@ResidenceFamilyRecyclerAdapter::editCallback.isInitialized)    {

                        editCallback.onEditAudit(s!!.trim().toString(),helper!!.adapterPosition - 1 )
                    }

                } else {
                    flag = true

                }


            }
        }
        etContent.addTextChangedListener(textWatcher)
        etContent.setTag(textWatcher)
    }
    private lateinit var editCallback: EditCallback

    fun setEditAuditCallBack(mItemCallBack: EditCallback) {
        editCallback = mItemCallBack
    }



    override fun onViewAttachedToWindow(holder: BaseViewHolder) {
        super.onViewAttachedToWindow(holder)
        val view = holder.getView<MediumBoldEditText>(R.id.et_content)
        if(null != view){
            val tag = view.getTag()
            view.addTextChangedListener(tag as TextWatcher)

        }

    }

    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)
        val view = holder.getView<MediumBoldEditText>(R.id.et_content)
        if(null != view) {
            val tag = view.getTag()
            view.removeTextChangedListener(tag as TextWatcher)

        }
    }


}

Activity

 residenceFamilyRecyclerAdapter.setEditAuditCallBack(EditCallback { content, position ->

            familyList[position].roomName = content
            if (null != residenceFamilyRecyclerAdapter.getViewByPosition(mRecyclerViewAudit,position+1,R.id.et_content)){
                val editText =  residenceFamilyRecyclerAdapter.getViewByPosition(mRecyclerViewAudit,position+1,R.id.et_content) as MediumBoldEditText

                if (!TextUtils.isEmpty(content)) {

                    editText.editTextWidth = 1f
                }else{
                    editText.editTextWidth = 0f
                }
            }


        })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值