Android ListView选择动画

Exe*_*sis 5 android android-animation android-listview listview-adapter

当用户在列表视图中选择元素时,如何设置动画?

我正在创建自己的listview适配器来设置具有粉红色背景的偶数行和具有紫色背景的奇数行.唯一的问题是我不确定如何为用户点击("触摸")元素设置动画.

我想到实现OnTouchListener并在选择时将背景更改为绿色但是由于OnTouchListener正在实现,我在行内部的按钮可能不再有效.这是真的?

码:

public class MyAdapter extends BaseAdapter {

    public View getView(int position, View convertView, ViewGroup parent) {
        // position is the element's 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's more to do with the view
        return convertView;
    }
}
Run Code Online (Sandbox Code Playgroud)

nEx*_*are 2

将 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>
Run Code Online (Sandbox Code Playgroud)

这样,当选择列表项时,它将自动使用“所选”图形。