【发布时间】:2015-01-25 14:36:00
【问题描述】:
我有一个包含复杂布局项的列表视图。每个项目都有一个复选框和其他内容,例如 TextView 和 ImageView。为了解决复选框的滚动问题,我将标签设置为
holder.checkBox.setTag(this.getItem(position).getId());
这样,当我上下滚动时,复选框的状态会保持不变。但是,当我删除一个项目时,假设它是第一行,然后我调用 API 再次加载列表,第一行(之前是第二行)的复选框仍然被选中。重新加载数据后如何重置每个复选框的状态?
更新
我的视图
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if ( convertView == null ) {
holder = new ViewHolder();
convertView = LayoutInflater.from(this.getContext()).inflate(
R.layout.list_item_conversation, parent, false);
holder.avatar = (ImageView) convertView.findViewById(R.id.img_conver_ava);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_conversation);
holder.msg = (TextView) convertView.findViewById(R.id.tv_conver_msg);
holder.sender = (TextView) convertView.findViewById(R.id.tv_conver_sender);
holder.timeSent = (TextView) convertView.findViewById(R.id.tv_conver_time_sent);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.checkBox.setTag(this.getItem(position).getId());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Save item's id into sharedPreference
}
});
return convertView;
}
我的布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/cb_padding">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/et_forget_pw_margin_top"
android:id="@+id/cb_conversation"
android:button="@drawable/rounded_checkbox_conversation"
android:paddingLeft="@dimen/cb_padding"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_conver_ava"
android:layout_width="@dimen/img_conver_ava"
android:layout_height="@dimen/img_conver_ava" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/cb_padding">
<TextView
android:id="@+id/tv_conver_sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Large"/>
<TextView
android:id="@+id/tv_conver_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_conver_time_sent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small"/>
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
请从您的 xml 中粘贴您的自定义适配器和复选框
-
可以通过写notifyDatasetChanged()来刷新listVIew;在您要删除它的适配器中
-
stackoverflow.com/a/27148650/2819233 刷新listView,问题就解决了
-
我的情况是我允许用户先选择多行,然后调用 API 删除这些项目。之后,我调用另一个 API 来获取数据以使用 notifyDataSetChanged() 再次填写列表。数据已刷新,但复选框的状态未刷新。
标签: android checkbox android-listview