这是视图回收。尝试使用 ViewHolder 模式并为每个 getView 调用设置项目状态。如果你想这样做,你必须在你的复杂对象中保持视图状态。例如,您的复杂对象包含 TextView、ImageView 和 CheckBox
public View getView(int position, View convertView, ViewGroup parent) {
ComplexObject co = objects.get(position);
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(co.getText());
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
holder.checkbox.setChecked(co.isChecked());
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
co.setChecked(isChecked);
}
});
return convertView;
}
protected class ViewHolder{
TextView text;
ImageView icon;
CheckBox checkbox;
}
希望它会有所帮助