RecyclerView中EditText数据混乱,FoldingCell折叠布局第一次点击有问题
2017-09-11 17:42
459 查看
出错效果图:
Bean包
改完之后EditText混乱的问题没有了,但是我是用的是FoldingCellAndroid这个可以折叠的开源库传送门:https://github.com/Ramotion/folding-cell-android
折叠起来的布局在屏幕上消失的时候就重新展开了所以上面解决冲突的方法有一定的局限性.
9.14
更改:使用复用功能,利用Bean储存EditText中的数据. 参考:http://blog.csdn.net/w6718189/article/details/76189046
代码:
可以看到第一次点击的时候是重新展开了一次
解决这个问题要让展开的布局设置Visibility = "gone" 折叠的布局设置Visibility = "visible"
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"
相关文章推荐
- RecyclerView + EditText item 复用造成数据混乱
- Android RecyclerView 中多Item的EditText焦点混乱,数据错乱的解决方法(更新)
- 处理RecyclerView中嵌套EditText数据显示混乱
- 解决RecyclerView实现聊天界面,但点击下面的EditText后弹出的输入法会遮盖RecyclerView内容的方法
- 基础向:recyclerView 内嵌 edittext checkbox 焦点获取、数据紊乱以及数据保存问题
- RecyclerView和EditText焦点冲突和输入法软键盘把布局顶出屏幕之外的解决
- 在RecyclerView中有多个edittext防止条目过多时滑动recyclerview后edittext内输入内容混乱BUG解决
- RecyclerView+EditText 动态搜索数据
- RecyclerView EditText 数据错乱问题
- RecyclerView嵌套多个布局,多个editText,解决文本输入框上下交换位置中的值问题
- 关于listview或者recyclerview里面的edittext数据保存问题
- RecyclerView展示数据 点击图标切换界面布局
- Android - VGA,ADB,DDMS,安卓开发环境目录结构,安卓目录结构,常见布局,点击事件的几种写法,控件(Button/TextView/ImageView/EditText)
- Android 解决ListView里面多套布局多个EditText数据混乱问题
- 关于EditTextView点击空白处,隐藏软键盘。适用于ScrollView等各种布局
- RecyclerView之crash 和 EditText数据重置问题
- 仿微信 QQ聊天布局 顶部title固定,底部是EditText,中间是RecyclerView,弹出软键盘,RecyclerView上移,标题不动
- RecyclerView多布局与Item点击事件
- Recyclerview添加头布局和尾布局、item点击事件详解
- 点击 TextView 弹出 时间设置框 DatePicker 、 DatePickerDialog,用于Sqlite db 获取数据