listview内部的imageview fortouch for change image不起作用

时间:2013-10-31 16:58:17

标签: android listview touch

我有listview的自定义行。使用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;
}}

有人能帮帮我吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

使用ImageView

的选择器

说:image_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">     
     <item android:state_selected="true" android:drawable="@drawable/button_yellow_down" />    
     <item android:state_pressed="true" android:drawable="@drawable/button_yellow_down" />
     <item android:drawable="@drawable/button_yellow" />
</selector>

并像

一样使用它
<ImageView 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"    
     android:background="@drawable/image_selector" /> 

希望这会对你有所帮助。

答案 1 :(得分:0)

我解决了:

http://www.anddev.it/index.php?topic=1087.0

我希望它会对某人有所帮助......