我有一个带有复杂布局项目的列表视图。每个项目都有一个复选框和其他东西,如TextView和ImageView。要使用复选框解决滚动问题,我将settag设为
holder.checkBox.setTag(this.getItem(position).getId());
这样,当我向上和向下滚动时,会保留复选框的状态。但是,当我删除一个项目时,让我们说它是第一行,然后我要求API再次加载列表,仍然检查第一行(之前是第二行)的复选框。重新加载数据后,如何重置每个复选框的状态?
更新
我的getView
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>
答案 0 :(得分:0)
这是我的代码,plz确实理解它,因为这段代码使用的是我的变量,但它确实告诉了你愿意找到的senarioa:
基本上下面代码正在做的是当点击chkBox时它将项目添加到收藏夹并在unchked时删除所以plz找到下面的getView()n适配器代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Planet to display
IncidentItems item = mResult.get(position);
TextView graphName;
TextView colorPanel;
CheckBox checkBox;
ImageView icon;
// Create a new row view
if (convertView == null) {
convertView = mInflator.inflate(R.layout.home_list_row, null);
// Find the child views.
graphName = (TextView) convertView.findViewById(R.id.textView1);
checkBox = (CheckBox) convertView.findViewById(R.id.favorite_icon);
colorPanel = (TextView) convertView
.findViewById(R.id.tv_color_panel);
icon = (ImageView) convertView.findViewById(R.id.icon);
// Optimization: Tag the row with it's child views, so we don't have
// to
// call findViewById() later when we reuse the row.
convertView
.setTag(new Holder(graphName, checkBox, colorPanel, icon));
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
final IncidentItems planet = (IncidentItems) cb.getTag();
// ////////////////////////////////////////////////////////////
SharedPreferences mAppSharedPref;
mAppSharedPref = mContext.getSharedPreferences(
"AppSharedPref", Context.MODE_PRIVATE);
final Editor mEditor;
mEditor = mAppSharedPref.edit();
final Gson gson = new Gson();
Type type = new TypeToken<List<FavoiteGraphInfo>>() {
}.getType();
favoiteGraphInfoList = gson.fromJson(
mAppSharedPref.getString("favorite", ""), type);
if (favoiteGraphInfoList == null)
favoiteGraphInfoList = new ArrayList<FavoiteGraphInfo>();
if (cb.isChecked()) {
// chekbox is chked add to favorite
Toast.makeText(mContext,
"added to favorite",
Toast.LENGTH_SHORT).show();
}
});
/* end prompting user */
} else {
// Add item to favorite when chked
Toast.makeText(mContext, "added to favorite",
Toast.LENGTH_SHORT).show();
}
} else {
// remove item when it is uncheaked
Toast.makeText(mContext, "Removed from Favorites",
Toast.LENGTH_LONG).show();
}
// ////////////////////////////////////////////////////////////
planet.setChecked(cb.isChecked());
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call
// findViewById().
Holder viewHolder = (Holder) convertView.getTag();
checkBox = viewHolder.getCheckBox();
graphName = viewHolder.getGraphName();
colorPanel = viewHolder.getColorPanel();
icon = viewHolder.getIcon();
}
// Tag the CheckBox with the Planet it is displaying, so that we can
// access the planet in onClick() when the CheckBox is toggled.
checkBox.setTag(item);
// Display planet data
checkBox.setChecked(item.isChecked());
graphName.setText(item.getGeneric_name());
// set font to textview
Typeface font = Typeface.createFromAsset(mContext.getAssets(),
"Simplified_Rg.ttf");
graphName.setTypeface(font);
// set colors
colorPanel.setBackgroundColor(mContext.getResources().getColor(
GRID_COLORS[position % 6]));
// set icons
final String graphType = item.generic_type;
icon.setBackgroundResource(R.drawable.pie);
return convertView;
}
持有人类别:
public class Holder {
private TextView graphName;
private TextView colorPanel;
private CheckBox checkBox;
private ImageView icon;
public Holder() {
}
public Holder(TextView tvName, CheckBox chkBov, TextView tvColorPanel,
ImageView icn) {
this.checkBox = chkBov;
this.graphName = tvName;
this.colorPanel = tvColorPanel;
this.icon = icn;
}
public TextView getGraphName() {
return graphName;
}
public void setGraphName(TextView graphName) {
this.graphName = graphName;
}
public TextView getColorPanel() {
return colorPanel;
}
public void setColorPanel(TextView colorPanel) {
this.colorPanel = colorPanel;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public ImageView getIcon() {
return icon;
}
public void setIcon(ImageView icon) {
this.icon = icon;
}
}