RecyclerView EditText 数据错乱问题

因为之前用第三方封装好的适配器 一般情况下也没发现有问题,但是目前也碰到了这个问题  。按照通常方法如果你调用了notifyDataSetChanged

你直接Textwatcher里面直接修改bean类还是没有用,因为系统会重新绘制。所以导致数据错乱 通过查询一下网友的解决方法  我也试了一下 我发现我通过一个网友所提供的方法解决了 直接贴代码:

 //完整代码 

       if (holder instanceof EditTextItemHolder) {
            //1、为了避免TextWatcher在第2步被调用,提前将他移除。
            if (((EditTextItemHolder) holder).et_text.getTag() instanceof TextWatcher) {
                ((EditTextItemHolder) holder).et_text.removeTextChangedListener((TextWatcher) (((EditTextItemHolder) holder).et_text.getTag()));
            }


            // 第2步:移除TextWatcher之后,设置EditText的Text。 
            ((EditTextItemHolder) holder).et_text.setText(bean.getValue());




            TextWatcher watcher = new TextWatcher() {
                @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 (TextUtils.isEmpty(editable.toString())) {
                        bean.setValue("");
                    } else {
                        bean.setValue(editable.toString());
                    }
                }
            };
            ((EditTextItemHolder) holder).et_text.addTextChangedListener(watcher);
            ((EditTextItemHolder) holder).et_text.setTag(watcher);
        }


作者:david_zhw
链接:http://www.jianshu.com/p/bd805630b45b

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A RecyclerView is a powerful and flexible view for displaying collections of data. It provides a way to efficiently render large lists of items that can be scrolled smoothly, and it also supports various types of layout managers for laying out items in different ways. To add an EditText to a RecyclerView item, you need to create a custom layout for the item that includes an EditText view. Here is an example of a simple layout for a RecyclerView item that includes an EditText: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title"/> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter text"/> </LinearLayout> ``` In your RecyclerView adapter, you need to define a ViewHolder class that holds references to the views in the item layout. Here is an example of a ViewHolder class that holds references to the TextView and EditText views: ``` public class MyViewHolder extends RecyclerView.ViewHolder { public TextView title; public EditText editText; public MyViewHolder(View itemView) { super(itemView); title = itemView.findViewById(R.id.title); editText = itemView.findViewById(R.id.edit_text); } } ``` In the onBindViewHolder() method of your adapter, you can set the text of the EditText view based on the data for the item: ``` @Override public void onBindViewHolder(MyViewHolder holder, int position) { MyData data = mDataList.get(position); holder.title.setText(data.getTitle()); holder.editText.setText(data.getText()); } ``` You can also add a TextWatcher to the EditText view to capture changes made by the user: ``` holder.editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { // Do nothing } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { // Update the data for the item mDataList.get(position).setText(charSequence.toString()); } @Override public void afterTextChanged(Editable editable) { // Do nothing } }); ``` Note that if you have a large number of items in your RecyclerView, you may want to consider using a custom view holder pool to improve performance. This involves creating a pool of view holders that can be reused when scrolling through the list, rather than creating new view holders for each item.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值