我有一个列表视图的自定义行。带有 TexView 和 ImageView。我希望单击图像会像按钮一样改变颜色。我曾尝试在我的适配器中使用 setOnTouchListener,但效果不佳。
public class ListaAreeAdapter extends ArrayAdapter<Aree> {
public ListaAreeAdapter(Context context, int textViewResourceId, List aree) {
super(context, textViewResourceId, aree);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null)
{
//LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_area, null);
viewHolder = new ViewHolder();
viewHolder.nome = (TextView)convertView.findViewById(R.id.item_area_nome);
viewHolder.cancella = (ImageView)convertView.findViewById(R.id.item_area_cancella);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
Aree area = getItem(position);
viewHolder.nome.setText(area.getNome());
viewHolder.cancella.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.i("toccolista", "tocco = "+String.valueOf(motionEvent));
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
((ImageView)view).setBackgroundResource(R.drawable.button_yellow_down);
break;
case MotionEvent.ACTION_CANCEL:
((ImageView)view).setBackgroundResource(R.drawable.button_yellow);
break;
case MotionEvent.ACTION_UP:
((ImageView)view).setBackgroundResource(R.drawable.button_yellow);
break;
}
return false;
}
});
return convertView;
}
private class ViewHolder {
public TextView nome;
public ImageView cancella;
}}
任何人都可以帮助我吗?提前致谢。