我有一个 ListView,每行有 2 个 TextView 和一个 CheckBox。
我有一个可绘制对象(一条线),一旦用户检查复选框,可绘制对象将在选中的复选框行的背景上缩放为动画,我就可以在我的自定义适配器上使用getView()方法,将drawable设置为背景动画,但它也会缩放2个TextView和CheckBox,我希望只有drawable背景可以缩放为动画,我该怎么做?
这是我在自定义适配器上的getView()方法:
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
// cache view fields into the holder
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
convertView.setTag(holder);
final Cursor cursor = getCursor();
final View rowView = convertView;
cursor.moveToPosition(position);
holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);
holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
rowView.setBackgroundResource(R.drawable.line);
rowView.startAnimation(drawLine);
ShowListActivity.PRIORITY++;
}
else {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
rowView.setBackgroundResource(0);
ShowListActivity.PRIORITY--;
}
}
});
if (holder.itemCheck.isChecked()) rowView.setBackgroundResource(R.drawable.line);
else rowView.setBackgroundResource(0);
return rowView;
}
private static class ViewHolder {
TextView itemTitle;
TextView itemQuantity;
CheckBox itemCheck;
}