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

在“列表”的所有元素中设置clickable, focusable, focusableInTouchModeto 。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_widthmatch_parent而不是wrap_content确保整行是可选的。

于 2015-12-14T17:21:37.977 回答
6

不幸的是,使用可聚焦视图来模拟项目选择并不是一个好的解决方案,因为:

  • notifyDataSetChanged调用时失去焦点
  • 当子视图可聚焦时,焦点是有问题的

我编写了一个基础适配器类来使用 RecyclerView 自动处理项目选择。只需从中派生适配器并使用带有 state_selected 的可绘制状态列表,就像使用列表视图一样。

我有一篇关于它的博客文章,但这里是代码:

public abstract class TrackSelectionAdapter<VH extends TrackSelectionAdapter.ViewHolder> extends RecyclerView.Adapter<VH> {
    // Start with first item selected
    private int focusedItem = 0;

    @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        // Handle key up and key down and attempt to move selection
        recyclerView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();

                // Return false if scrolled to the bounds and allow focus to move off the list
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                        return tryMoveSelection(lm, 1);
                    } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        return tryMoveSelection(lm, -1);
                    }
                }

                return false;
            }
        });
    }

    private boolean tryMoveSelection(RecyclerView.LayoutManager lm, int direction) {
        int tryFocusItem = focusedItem + direction;

        // If still within valid bounds, move the selection, notify to redraw, and scroll
        if (tryFocusItem >= 0 && tryFocusItem < getItemCount()) {
            notifyItemChanged(focusedItem);
            focusedItem = tryFocusItem;
            notifyItemChanged(focusedItem);
            lm.scrollToPosition(focusedItem);
            return true;
        }

        return false;
    }

    @Override
    public void onBindViewHolder(VH viewHolder, int i) {
        // Set selected state; use a state list drawable to style the view
        viewHolder.itemView.setSelected(focusedItem == i);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);

            // Handle item click and set the selection
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Redraw the old selection and the new
                    notifyItemChanged(focusedItem);
                    focusedItem = mRecyclerView.getChildPosition(v);
                    notifyItemChanged(focusedItem);
                }
            });
        }
    }
} 
于 2015-03-03T18:12:40.240 回答
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 回答
3

在您的项目“my_list_item.xml”中添加以下属性
android:descendantFocusability="blocksDescendants" 这对我有用!

于 2015-02-17T09:21:12.487 回答
1

您需要android:clickable="true"在元素 xml 中进行设置,如果您的视图内的某个视图中有更多选择器,您也需要在 android:duplicateParentState="true" 那里进行设置。这适用于预蜂窝 API。

于 2015-02-20T18:34:59.233 回答
0

正如许多其他人回答的那样,唯一的方法是结合选择器和新类来跟踪选择,但最好将此计算委托给适配器。库FlexibleAdapter为您跟踪选择,配置更改也是兼容的。

带有波纹的背景颜色现在可以在没有 XML 部分的情况下完成,而是在代码中管理动态数据。

最后,您可以在同一个库中使用许多功能,选择是您可以拥有的一个很小的基本功能。

请查看描述、Wiki 页面和完整的工作示例:https ://github.com/davideas/FlexibleAdapter

于 2015-07-20T20:12:39.757 回答
0

在您的设计或 xml 文件上只需放置以下几行。

android:clickable="true"
android:focusable="true"
android:background="?android:attr/activatedBackgroundIndicator"
于 2018-09-27T07:36:37.823 回答