简体   繁体   中英

Android touchlistener for customadapter listview

I'm have implemented a custom adapter for a listview with click listener. Now I would like to extend the code to include a touch listener to change the background color (ACTION_DOWN & ACTION_UP).

I follow an online example and this is my current code

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======");
}

How do I implement the touch listener in the same way (or it cannot be done this way?) So far, I have tried to register the touch listener inside the getView method but it takes away the click listener and I too have no idea why it doesn't work. This is the touch listener I am trying to implement:

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;
    }
}

This is my listview 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>

this is my adapter 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>

newly added (accordingly to comment)

<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>

If you just wanted to do some backgroundColor change, then i will suggest you to create listSelector using XML. It's so easy and can make the view much cute using gradients.

See how it works Custom selector for list background

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM