Android 自定义适配器列表视图的触摸监听器

Android touchlistener for customadapter listview

我已经为带有点击侦听器的列表视图实现了自定义适配器。现在我想扩展代码以包含一个触摸侦听器来更改背景颜色 (ACTION_DOWN & ACTION_UP)。

我按照在线示例进行操作,这是我当前的代码

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    ViewHolder holder;

    if(convertView==null){

        /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
        vi = inflater.inflate(R.layout.sharer, null);

        /****** View Holder Object to contain tabitem.xml file elements ******/

        holder = new ViewHolder();
        holder.text1 = (TextView) vi.findViewById(R.id.text1);
        holder.text2=(TextView)vi.findViewById(R.id.text2);

        /************  Set holder with LayoutInflater ************/
        vi.setTag( holder );
    }
    else
        holder=(ViewHolder)vi.getTag();

    if(data.size()<=0)
    {
        holder.text1.setText("No Data");

    }
    else
    {
        /***** Get each Model object from Arraylist ********/
        tempValues=null;
        tempValues = ( Sharer ) data.get( position );

        /************  Set Model values in Holder elements ***********/

        holder.text1.setText( tempValues.getName() );
        holder.text2.setText(String.valueOf(tempValues.getAmount()));

        /******** Set Item Click Listener for LayoutInflater for each row *******/

        vi.setOnClickListener(new OnItemClickListener( position ));
    }

    return vi;
}

private class OnItemClickListener implements OnClickListener
{
    private int mPosition;

    OnItemClickListener(int position)
    {
        mPosition = position;
    }

    @Override
    public void onClick(View v) {
        SharerActivity act  = (SharerActivity) activity;
        act.onItemClick(mPosition);
    }
}

@Override
public void onClick(View v) {
    Log.v("SharerAdapter", "====== Row Button Click======");
}

如何以相同的方式实现触摸侦听器(或者不能以这种方式完成?)到目前为止,我已经尝试在 getView 方法中注册触摸侦听器,但它带走了点击侦听器,我也不知道为什么它不起作用。这是我要实现的触摸监听器:

private class OnTouchListener implements View.OnTouchListener
{
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            v.setBackgroundColor(Color.GREEN);
            return true;
        }
        else if (event.getAction() == MotionEvent.ACTION_UP)
        {
            v.setBackgroundColor(Color.TRANSPARENT);
            return true;
        }

        return false;
    }
}

这是我的列表视图xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ListView
    android:id="@+id/sharerlistview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:listSelector="@drawable/item"/>

</RelativeLayout>

这是我的适配器xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="0dip" android:layout_gravity="top"

>
<TableRow>
    <TextView
        android:id="@+id/text1"
        android:layout_height="75px"
        android:layout_width="wrap_content"
        android:layout_weight="2"     android:layout_gravity="left|center_vertical"
        android:textSize="16sp"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="4dip"
        android:textColor="#000000"
        android:layout_span="1"
        />
    <TextView
        android:text=""
        android:id="@+id/text2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="left|center_vertical"
        android:textSize="16sp"
        android:textColor="#000000"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="4dip"
        android:gravity="left"/>
</TableRow>
</TableLayout>

新增(根据评论)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/bg"/>
</selector>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/holo_red_dark"/>
</shape>

如果您只想更改背景颜色,那么我建议您使用 XML 创建 listSelector。非常简单,可以使用渐变使视图更加可爱。

看看它是如何工作的Custom selector for list background