41

私はRecyclerView以下のようなものを使用しています:

<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="320dp"
    android:layout_height="match_parent"/>

と私のリスト項目:

<LinearLayout  android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_medium_high">
    <com.basf.suvinil.crie.ui.common.widget.CircleView
        android:id="@+id/circle"
        android:layout_width="22dp"
        android:layout_height="22dp"/>
    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="57.5dp"/>
</LinearLayout>

この部分を詳しく見てください。これandroid:background="@drawable/selector_medium_high"は通常のセレクターです。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/background_high" android:state_activated="true"/>
    <item android:drawable="@color/background_high" android:state_pressed="true"/>
    <item android:drawable="@color/background_high" android:state_checked="true"/>
    <item android:drawable="@color/background_high" android:state_focused="true"/>
    <item android:drawable="@color/background_medium"/>
</selector>

しかし、このコードを実行すると、行に触れても背景色に変化はありません....

4

10 に答える 10

53

"list"のすべての要素にclickablefocusablefocusableInTouchModeを設定します。trueRecyclerView

于 2014-08-19T21:47:26.503 に答える
35

追加 :

android:background="?android:attr/selectableItemBackground"

item.xml

于 2016-01-28T11:02:53.963 に答える
17

私のように、これがうまくいかない場合は、次のコードを使用してください。

android:foreground="?android:attr/selectableItemBackground"

コツはandroid:foreground属性にある...

于 2016-07-14T09:37:14.127 に答える
13

ルート レイアウトに追加android:background="?android:attr/selectableItemBackground"するとうまくいくようです (デフォルトの選択色が必要な場合)。my_list_item.xml

また、行全体が選択可能であることを確認するのandroid:layout_widthではmatch_parentなく、ルート レイアウトが確実であることを確認してください。wrap_content

于 2015-12-14T17:21:37.977 に答える
4
    viewHolder.mRlPrince.setOnTouchListener(new View.OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction()==MotionEvent.ACTION_DOWN){
                viewHolder.mRlPrince.setBackgroundColor(Color.parseColor("#f8f8f8"));
            }if (event.getAction()==MotionEvent.ACTION_UP){
                viewHolder.mRlPrince.setBackgroundColor(Color.WHITE);
            }

            return false;
        }
    });
于 2016-05-04T06:15:04.637 に答える
0

他の多くの人が答えたように、唯一の方法は、セレクターと新しいクラスを組み合わせて選択を追跡することですが、この計算をアダプターに委任することをお勧めします。ライブラリFlexibleAdapterが選択を追跡し、構成の変更にも対応しています。

リップルを使用した背景色は、XML 部分なしで実行できるようになりましたが、動的データを管理するコードで実行できます。

最後に、同じライブラリで多くの機能を使用できます。選択は、使用できる小さな基本機能です。

説明、Wiki ページ、完全な動作例をご覧ください: https://github.com/davideas/FlexibleAdapter

于 2015-07-20T20:12:39.757 に答える