0

以下は私のソースコードです、

isPressed、も試しisClickedましたが、まだ機能しません。

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        // This a new view we inflate the new layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.my_item_layout, parent, false);
    }
    MyItem myItem = getItem(position);


    TextView direction = (TextView) convertView.findViewById(R.id.direction);
    direction.setText(myItem .getDirection());

    if(convertView.isSelected()){
        convertView.setBackgroundResource(R.drawable.list_select_bar);
        setTextColor(convertView, textIDs , R.color.white);
    }else{
        convertView.setBackgroundResource(R.color.light_white);
        setTextColor(convertView, textIDs , R.color.black);
    }

    return convertView;
}

実際には、convertView チェック ブロックを削除すると、リストビューに onItemClickListener を登録するだけで済みます...しかし、これを行うと、getView メソッドが無意味になるようです。私はこの問題にとても苦労しています。

4

4 に答える 4

0

まず、サムが私に役立つヒントをくれたことに感謝します。以下は私の解決手順です。

セレクターファイルを書きます。以下はその内容です。次に、このlist_background.xmlを描画可能なフォルダーに入れます

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_select_bar" android:state_pressed="true"/><!--pressed -->
<item android:drawable="@color/light_white"/><!-- default --></selector>

次に、アイテム レイアウト xml で、次のように list_background を使用します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

... android:background="@drawable/list_background" ... >

それでおしまい。ソース コードで呼び出されるクリック リスナーやその他のメソッドはありません。

于 2012-11-15T10:53:44.840 に答える
0

getViewリスト ビューのレンダリング中にのみ呼び出されます。アイテムが選択またはクリックされたときに呼び出されません。

おそらくあなたが必要とするのは使用することですconvertView.setOnFocusChangeListener

于 2012-11-15T04:02:52.903 に答える
0

getView()表示されていなかったビューを作成 (または再作成) します。したがって、このような新しいビューを選択したり、押したり、クリックしたりすることはできません。

Color State Selectorが必要だと思います。


(上記のリンクから)次の場所に保存された XML ファイルres/color/button_text.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

このレイアウト XML は、カラー リストをビューに適用します。

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />

または、ListViews、GridViews などのListView State Selectorで同様の方法を使用できます。

于 2012-11-15T04:10:55.110 に答える
0

行に指を置いたときにのみ背景色を変更し、指を離したときに元に戻すことを想定していますか? これを行うには、行項目に OnTouchListener を追加するだけです。

convertView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            convertView.setBackgroundResource(R.drawable.list_select_bar);
        } else if (event.getAction() == MotionEvent.ACTION_UP 
                || event.getAction() == MotionEvent.ACTION_CANCEL) {
            convertView.setBackgroundResource(R.color.light_white);
        }
        return false;
    }
);
于 2012-11-15T06:49:28.827 に答える