5

ユーザーがリストビューで要素を選択したときにアニメーションを設定するにはどうすればよいですか?

ピンクの背景の偶数行と紫色の背景の奇数行を設定する独自のリストビューアダプターを作成しています。唯一の問題は、要素をクリック (「タッチ」) するユーザーのアニメーションを設定する方法がわからないことです。

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

1 に答える 1

2

state_selected の項目が定義された StateListDrawable を使用します。

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

このようにして、リスト項目が選択されると、その「選択された」グラフィックが自動的に使用されます。

于 2011-02-15T16:16:31.363 に答える