近期开发需求:住宅收录,每一楼层户室上限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
}
}
})