那里有什么问题?我有一个列表视图,我想在点击列表项目时更改项目的背景。如果我选择一个项目,然后单击不同的项目,然后将背景切换到另一个。这是单一选择过程
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.customer_account_customview, null);
holder = new ViewHolder();
holder.tr = (TableRow)convertView.findViewById(R.id.tableRow);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.tr.setTag((Integer)position);
holder.tr.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view)
{
int tableRowPosition=(new Integer((Integer) view.getTag())).intValue();
Log.i("Position of Row click", ""+tableRowPosition);
holder.tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.abc));
}
});
class CustomerAccountAdapter extends BaseAdapter
{
public CustomerAccountAdapter() {
mInflater = LayoutInflater.from(CustomerAccount.this);
selectedIds = new ArrayList<Integer>();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist_invoices.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arraylist_invoices.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
vvw = convertView;
Log.i("inside", "getView()");
if (vvw == null)
{
vvw = mInflater.inflate(R.layout.customer_account_customview, null);
holder = new ViewHolder();
holder.tableRow = (TableRow)vvw.findViewById(R.id.tableRow10);
vvw.setTag(holder);
}
else
{
holder = (ViewHolder) vvw.getTag();
}
holder.tableRow.setTag((Integer)position);
holder.tableRow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view)
{
// TODO Auto-generated method stub
Integer myIntegerObject=new Integer((Integer) view.getTag());
tableRowPosition=myIntegerObject.intValue();
Log.i("Position of Row click", ""+tableRowPosition);
// notifyDataSetChanged();
// holder.tableRow.setBackgroundResource(R.drawable.table_heading_shape);
// holder.tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.cancel_big_butt));
notifyDataSetChanged();
// holder.tableRow.setBackgroundResource();
}
});
return vvw;
}
}
private class ViewHolder
{
TextView txtDate, txtInv, txtAmt, txtPaid, txtBal,txtDays;
TableRow tableRow;// All TextView inside TableRow
}
答案 0 :(得分:1)
您必须存储每个列表项的背景状态,并在getView方法中引用它来决定应用哪个背景。因此,您可以将成员变量添加到列表中的任何类,或者您可以创建第二个数组,其元素数量与列表相同,并使用项目位置来获取状态。
我有点困惑,因为你发布了两个不同的类做类似的事情。所以这可能与你的类完全不匹配,但我会在你的ViewHolder类中添加一个布尔值,如下所示:
private class ViewHolder
{
TextView txtDate, txtInv, txtAmt, txtPaid, txtBal,txtDays;
TableRow tableRow;// All TextView inside TableRow
boolean backgroundChanged = false;
}
然后,在return vvw;
之前,放
holder.tr.setBackgroundDrawable( getResources().getDrawable(
holder.backgroundChanged? R.drawable.theDefaultBackground : R.drawable.abc
));
然后在你的onClickListener中,你需要在按下时存储这个值:
public void onClick(View view)
{
int tableRowPosition=(new Integer((Integer) view.getTag())).intValue();
Log.i("Position of Row click", ""+tableRowPosition);
holder.tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.abc));
holder.backgroundChanged = true;
}
顺便说一句,每次视图滚动到屏幕上时,您都会创建相同onClickListener的新副本,这可能会使动画不那么流畅。我建议创建一个应用于getView中所有视图的onClickListener。