单击时突出显示RecyclerView中的选定项目

时间:2018-08-21 12:51:53

标签: android android-recyclerview ontouchlistener

我得到一个RecyclerView,并且我弄清楚了如何在它们被点击并像告诉here时突出显示它们。

但这会突出显示点击后的项目。我想在普通的ListView中有一些东西。因此,单击时应突出显示该项目。这就是为什么我改用OnTouchListener的原因。

@Override
    public boolean onTouch(View v, MotionEvent event) {
        int adapterPosition = getAdapterPosition();
        if (adapterPosition == RecyclerView.NO_POSITION) return false;

        adapter.notifyItemChanged(adapter.getSelectedPos());
        adapter.setSelectedPos(adapterPosition);
        adapter.notifyItemChanged(adapter.getSelectedPos());

        if(event.getAction() == MotionEvent.ACTION_UP){
            clicks.onItemSelected(adapterPosition);
            return true;
        }

        return false;
    }

但是我的问题是,我还想知道用户何时向上移动手指,因此单击鼠标时我可以做我想做的事情。但不幸的是,这行不通。我该如何实现?

3 个答案:

答案 0 :(得分:0)

尝试将此属性添加到回收者视图的XML

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

如果这不起作用,请在这里查看可能对您有帮助:

How to properly highlight selected item on RecyclerView?

  <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
    />

让我们假设这是您的项目视图,还将此属性android:background =“?android:attr / selectableItemBackground”也添加到父对象,在这种情况下,它是相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"/>
</RelativeLayout>

答案 1 :(得分:0)

要在单击时为项目添加背景色,可以添加 父项视图的 android:background 属性(在适配器布局内声明)。

在drawable内添加 background_selector.xml

| ID  | Name  | Surname | BranchCode | CountryCode  | PersonUnit
+-----+-------+---------+------------+--------------+----------
| 83  | Jeck  | Payl   |   100       |    087       |  196
| 65  | Alb   | Payl   |   170       |    983       |  325

在适配器布局内像下面一样在xml上面使用

答案 2 :(得分:0)

您提供的代码,我认为突出显示所选项目不是正确的方法。您的代码会多次通知不必要的项目。

这可以通过选择器可绘制来完成。有 many answers 指南,介绍如何使用选择器背景。

但这是问题所在

selectableItemBackground 仅适用于触摸,而不适用于选择。所以这是一个hack。

我们将前景设置为?selectableItemBackground,并在同一视图上设置自定义背景选择器。

我创建了一个满足您要求的示例。

  • 首先创建一个背景选择器,该选择器在禁用时设置透明颜色,在激活时设置颜色。

activated_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true">
        <color android:color="@color/colorPrimary"/>
    </item>
    <item>
        <color android:color="@android:color/transparent"/>
    </item>
</selector>
  • 使用此选择器作为项目的背景。还将前景设置为?selectableItemBackground

row.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/activated_color_selector"
    android:foreground="?selectableItemBackground">

    // other views inside

</LinearLayout>
  • 您的模型类将保存选定的布尔值。

Model.java

public class BaseModel extends BaseObservable {
    private boolean selected;
    // setter getter
}
  • 将模型选择字段附加到视图。单击时还可以反转选定的字段。

内部RecyclerView适配器

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    final BaseModel model = list.get(position);
    holder.linearLayout.setActivated(model.isSelected());
    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            model.setSelected(!model.isSelected());
            notifyItemChanged(position);
        }
    });
}

全部完成!

相关问题