Android ListView Select Animation

How to set up animation when the user selects an item in the list?

I create my own listview adapter to set even lines with a pink background and odd lines with a purple background. The only problem is that I'm not sure how to set up the animation for the user to click (“touch”) the item.

I was thinking about implementing OnTouchListener and changing the background to green when it is selected. BUT I have buttons inside the lines that can no longer work due to the implementation of OnTouchListener. It's true?

the code:

public class MyAdapter extends BaseAdapter {

    public View getView(int position, View convertView, ViewGroup parent) {
        // position is the element id to use
        // convertView is either null -> create a new view for this element!
        //                or not null -> re-use this given view for element!
        // parent is the listview all the elements are in    

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.your_layout, null);

            // here you must do whatever is needed to populate the elements of your
            // list element layout
            ...
        } else {
            // re-use the given convert view

            // here you must set all the elements to the required values
        }

        // your drawable here for this element 
        convertView.setBackground(...);

        // maybe here more to do with the view
        return convertView;
    }
}
+5
source share
1 answer

StateListDrawable state_selected.

<?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/selected" />
    ...Other States...
    <item android:drawable="@drawable/normal" />
</selector>

, , "" .

+2

All Articles