RecyclerView的item有EditText時複用問題,及RecyclerView.Adapter的onBindViewHolder參數position不能final。

RecyclerView已經基本替代了ListView和GridView成爲開發中使用最多的控件,在使用的過程中遇到的問題在此記錄一下。
1,RecyclerView的item中有EditText時,當你在某個item的EditText輸入內容之後滑動的話,由於複用就會導致你輸入的內容出現的複用的新item中,再滑回去可能你剛纔輸入的內容就沒有了,這個肯定不行的。
解決辦法:a,最簡單的是把EditText換成TextView,寫一個單獨的輸入功能,例如一個能輸入的Dialog,每次點擊TextView彈窗輸入Dialog即可。b,定義一個數組存放每個item輸入的內容,這樣也可以保證順序不亂。
2,在RecyclerView.Adapter的onBindViewHolder方法中,假如有監聽器之類的東西里用到了局部變量是需要把這些局部變量定義爲final,這裏如果用到position,我們通常不能把position設置爲final,因爲這樣可能會出問題,比如你的item中有CheckBox之類的控件,就會導致選中狀態出現問題,這個可以想辦法解決,但是不是問題的根本所在,我們通常可以用holder.getAdapterPosition()來代替position,這樣就不會出現問題了。

結合上面問題,代碼如下:

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHolder> {

    private Context context;
    private ArrayList<ListBean> listBeans;
    private String[] inputs;

    public ListAdapter(Context context, ArrayList<ListBean> listBeans) {
        this.context = context;
        this.listBeans = listBeans;
        inputs = new String[listBeans.size()];
    }

    @NonNull
    @Override
    public ListHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        return new ListHolder(LayoutInflater.from(context).inflate(R.layout.item_recyclerview, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ListHolder holder, int position) {
        final ListBean listBean = listBeans.get(position);
        if (listBean != null) {
            holder.tvTitle.setText(listBean.title);
            holder.checkbox.setChecked(listBean.isChecked);
            holder.etInput.setText(inputs[holder.getAdapterPosition()]);
            holder.etInput.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    inputs[holder.getAdapterPosition()] = s.toString();
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    listBeans.get(holder.getAdapterPosition()).isChecked = isChecked;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return listBeans == null ? 0 : listBeans.size();
    }

    class ListHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvTitle)
        TextView tvTitle;
        @BindView(R.id.etInput)
        EditText etInput;
        @BindView(R.id.checkbox)
        CheckBox checkbox;

        public ListHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章