【问题标题】:Android ListView Select AnimationAndroid ListView 选择动画
【发布时间】:2010-12-21 22:39:11
【问题描述】:

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

我正在制作我自己的列表视图适配器,以将偶数行设置为粉红色背景,将奇数行设置为紫色背景。唯一的问题是我不确定如何为用户单击(“触摸”)元素设置动画。

我想过实现 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;
    }
}

【问题讨论】:

    标签: android android-listview android-animation listview-adapter


    【解决方案1】:

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

    这样,当列表项被选中时,它会自动使用“选中”的图形。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      相关资源
      最近更新 更多