RecyclerView的item中有EditView问题

RecyclerView的item中有EditText,对EditText进行编辑后滑动列表,有时编辑的不起作用依旧显示之前的内容(缓存),有时编辑多条时显示内容错乱,每次滑动都会有不同的内容呈现,现象有点奇葩;下面是解决此问题的一种方式,以供参考;

RecyclerView源码中有这么一段话:

意思提示我们避免在item中使用EditText,并且小心处理能够获取到光标的view;
工作中之前也遇到过RecyclerView的item中EditeText编辑后滑动recyclerview出现错误的问题;

/* RecyclerView is calculating a scroll.
* If there are too many of these in Systrace, some Views inside RecyclerView might be causing
* it. Try to avoid using EditText, focusable views or handle them with care.
*/

代码很简单抓住点子

xml文件:里面就一个EditText







类文件,还是贴上吧,

package com.example.jianghai.demo.recyclereditview;

import android.os.Bundle;   
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.jianghai.demo.R;
import java.util.ArrayList;
import java.util.List;

 public class RecyclerViewEditViewActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerViewEditTextAdapter editTextAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_editview);
        initView();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.recycler_edit);
        editTextAdapter = new RecyclerViewEditTextAdapter(getData(), this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(editTextAdapter);
    }

    private List getData() {
        List list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("editText_0" + i);
        }
        return list;
    }  
}

主要是Adapter文件

package com.example.jianghai.demo.recyclereditview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import com.example.jianghai.demo.R;
import com.example.jianghai.demo.adapter.MainAdapter;

import java.util.List;

public class RecyclerViewEditTextAdapter extends RecyclerView.Adapter {

    private List list;
    private Context mContext;

    public RecyclerViewEditTextAdapter(List list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
    }

    @Override
    public RecyclerEditViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_recyclerview, null);
        return new RecyclerEditViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerEditViewHolder holder, int position) {
        holder.editText.setText(list.get(position));
        if (holder.customTextWatcher == null) {
            holder.customTextWatcher = new CustomTextWatcher(position);
        }
        holder.customTextWatcher.setIndex(position);
        holder.editText.addTextChangedListener(holder.customTextWatcher);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    final class CustomTextWatcher implements TextWatcher {

        int index;

        public CustomTextWatcher(int index) {
            this.index = index;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable != null) {
                list.set(getIndex(), editable.toString());
            }
        }
    }


    class RecyclerEditViewHolder extends RecyclerView.ViewHolder{

        private EditText editText;
        private CustomTextWatcher customTextWatcher;
        public RecyclerEditViewHolder(View itemView) {
            super(itemView);
            editText = itemView.findViewById(R.id.item_et);
            customTextWatcher = null;
        }
    }

    @Override
    public void onViewRecycled(RecyclerEditViewHolder holder) {
        if (holder != null && holder.customTextWatcher != null) {
            holder.editText.removeTextChangedListener(holder.customTextWatcher);
            holder.customTextWatcher = null;
        }
        super.onViewRecycled(holder);
    }
}

你可能感兴趣的:(RecyclerView的item中有EditView问题)