RecyclerView中EditText数据混乱,FoldingCell折叠布局第一次点击有问题

出错效果图:


Bean包

public class RecyclerBean {
    String editContent;

    public String getEditContent() {
        return editContent;
    }

    public void setEditContent(String editContent) {
        this.editContent = editContent;
    }

    public RecyclerBean(String editContent) {
        this.editContent = editContent;
    }

    @Override
    public String toString() {
        return "RecyclerBean{" +
                "editContent='" + editContent + '\'' +
                '}';
    }
}
模拟数据

List<RecyclerBean> recyclerBeanList = new ArrayList<>();
    //添加数据
    public void addString() {
        for (int i = 0; i <= 20; i++) {
            recyclerBeanList.add(new RecyclerBean("this is:" + i));
        }

    }
在RecyclerView的Adapter中设置不复用
@Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        holder.setIsRecyclable(false);//设置不开启复用

        holder.editText.setText(list.get(position).getEditContent());

        holder.cell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.cell.toggle(false);
            }
        });
        holder.editText.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setEditContent(s.toString());
            }
        });
改完之后的效果:

改完之后EditText混乱的问题没有了,但是我是用的是FoldingCellAndroid这个可以折叠的开源库传送门:https://github.com/Ramotion/folding-cell-android

折叠起来的布局在屏幕上消失的时候就重新展开了所以上面解决冲突的方法有一定的局限性.

9.14

更改:使用复用功能,利用Bean储存EditText中的数据. 参考:http://blog.csdn.net/w6718189/article/details/76189046

代码:

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);
        }
FoldingCell折叠布局第一次点击有问题


可以看到第一次点击的时候是重新展开了一次

<com.ramotion.foldingcell.FoldingCell
        android:id="@+id/cell1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:clipChildren="false"
        android:clipToPadding="false">
       <!--展开之后的布局-->
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:visibility="gone"
            app:cardBackgroundColor="#0c5514"
            app:cardElevation="2dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginBottom="2dp"
                >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginLeft="3dp"
                        android:layout_marginRight="3dp"
                        android:layout_marginTop="10dp"
                        android:background="#FFFFFF" />

                    <View
                        android:layout_width="2dp"
                        android:layout_height="11dp"
                        android:layout_marginLeft="3dp"
                        android:layout_marginTop="10dp"
                        android:background="#FFFFFF" />

                    <View
                        android:layout_width="2dp"
                        android:layout_height="11dp"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="3dp"
                        android:layout_marginTop="10dp"
                        android:background="#FFFFFF" />

                    <TextView
                        android:id="@+id/textView8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginTop="2dp"
                        android:layout_toRightOf="@+id/linear3"
                        android:background="#0c5514"
                        android:text="TextView"
                        android:textColor="@android:color/white" />

                    <LinearLayout
                        android:id="@+id/linear3"
                        android:layout_width="10dp"
                        android:layout_height="10dp"
                        android:layout_marginStart="10dp"
                        android:layout_marginTop="7dp"
                        android:background="#0c5514"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="10dp"
                            android:layout_height="10dp"
                            android:background="@mipmap/down_arrow" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/linear4"
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="3dp"
                        android:layout_toRightOf="@+id/textView8"
                        android:background="#0c5514"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="15dp"
                            android:layout_height="15dp"
                            android:background="@mipmap/addview" />
                    </LinearLayout>
                </RelativeLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/textView8"
                    android:layout_marginLeft="0.5dp"
                    android:layout_marginRight="0.5dp"
                    android:background="@drawable/background"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:gravity="center_horizontal"
                            android:maxLines="2"
                            android:text="转换等级"
                            android:textColor="@android:color/white" />

                        <RelativeLayout
                            android:id="@+id/changeLevel"
                            android:layout_width="200dp"
                            android:layout_height="20dp"
                            android:layout_marginLeft="10dp"
                            android:background="@android:color/white"
                            >
                            <TextView
                                android:id="@+id/changeLevelText"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_toLeftOf="@+id/changeLevelImage"
                                android:gravity="center"
                                />
                            <ImageView
                                android:id="@+id/changeLevelImage"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@mipmap/xiala"
                                android:layout_alignParentRight="true"
                                android:layout_centerVertical="true"
                                />
                        </RelativeLayout>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:gravity="center_horizontal"
                            android:maxLines="2"
                            android:text="接到预先号令时间"
                            android:textColor="@android:color/white" />

                        <LinearLayout
                            android:layout_width="200dp"
                            android:layout_height="20dp"
                            android:layout_marginLeft="10dp"
                            android:background="@android:color/white"
                            android:gravity="right|center_vertical">

                            <TextView
                                android:id="@+id/receiveDate"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center_horizontal"
                                />
                        </LinearLayout>

                        <Button
                            android:id="@+id/receiveDateButton"
                            android:layout_width="50dp"
                            android:layout_height="20dp"
                            android:layout_marginLeft="15dp"
                            android:background="#1F6094"
                            android:text="获取"
                            android:textColor="@android:color/white"
                            android:textSize="15sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:gravity="center_horizontal"
                            android:maxLines="2"
                            android:text="人员出动数量"
                            android:textColor="@android:color/white" />

                        <LinearLayout
                            android:layout_width="200dp"
                            android:layout_height="20dp"
                            android:layout_marginLeft="10dp"
                            android:background="@android:color/white"
                            android:gravity="right|center_vertical">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@mipmap/xiala" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="人"
                            android:textColor="@android:color/white" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="7dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:gravity="center_horizontal"
                            android:maxLines="2"
                            android:text="启封装备开始时间"
                            android:textColor="@android:color/white" />

                        <LinearLayout
                            android:layout_width="200dp"
                            android:layout_height="20dp"
                            android:layout_marginLeft="10dp"
                            android:background="@android:color/white"
                            android:gravity="right|center_vertical">

                            <TextView
                                android:id="@+id/startDate"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center_horizontal"
                                />
                        </LinearLayout>

                        <Button
                            android:id="@+id/startDateButton"
                            android:layout_width="50dp"
                            android:layout_height="20dp"
                            android:layout_marginLeft="15dp"
                            android:background="#1F6094"
                            android:text="获取"
                            android:textColor="@android:color/white"
                            android:textSize="15sp" />
                    </LinearLayout>
                </LinearLayout>
                <View
                    android:layout_width="wrap_content"
                    android:layout_height="5dp"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <!--折叠的布局-->
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:visibility="visible"
            app:cardBackgroundColor="#0c5514"
            app:cardElevation="2dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="vertical"
                android:background="#0c5514"
                android:layout_marginBottom="5dp"
                >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp">

                    <View
                        android:id="@+id/view6"
                        android:layout_width="1dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_marginTop="10dp"
                        android:background="#FFFFFF" />

                    <View
                        android:id="@+id/view5"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_alignParentBottom="true"
                        android:layout_marginTop="5dp"
                        android:background="#FFFFFF" />

                    <View
                        android:id="@+id/view4"
                        android:layout_width="1dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="10dp"
                        android:background="#FFFFFF" />

                    <View
                        android:id="@+id/view3"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginTop="10dp"
                        android:background="#FFFFFF" />

                    <TextView
                        android:id="@+id/textView7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginTop="2dp"
                        android:layout_toRightOf="@+id/linear1"
                        android:background="#0c5514"
                        android:text="TextView"
                        android:textColor="@android:color/white" />

                    <LinearLayout
                        android:id="@+id/linear1"
                        android:layout_width="10dp"
                        android:layout_height="10dp"
                        android:layout_marginStart="10dp"
                        android:layout_marginTop="7dp"
                        android:background="#0c5514"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/imageView1"
                            android:layout_width="10dp"
                            android:layout_height="10dp"
                            android:background="@mipmap/down_arrow" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/linear2"
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="3dp"
                        android:layout_toRightOf="@+id/textView7"
                        android:background="#0c5514"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/imageView2"
                            android:layout_width="15dp"
                            android:layout_height="15dp"
                            android:background="@mipmap/addview" />
                    </LinearLayout>
                </RelativeLayout>
                <View
                    android:layout_width="wrap_content"
                    android:layout_height="5dp"/>
            </LinearLayout>
            </android.support.v7.widget.CardView>
    </com.ramotion.foldingcell.FoldingCell>

 解决这个问题要让展开的布局设置Visibility = "gone" 折叠的布局设置Visibility = "visible" 








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值