【问题标题】:Highlight selected item in RecyclerView WHILE clicking在单击时突出显示 RecyclerView 中的选定项目
【发布时间】:2018-08-21 12:51:52
【问题描述】:

我得到了一个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;
    }

但我的问题是,我还想知道用户何时向上移动手指,这样我可以在点击它时做我想做的事。但不幸的是,这不起作用。我怎样才能做到这一点?

【问题讨论】:

标签: android android-recyclerview ontouchlistener


【解决方案1】:

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

这可以通过一个可绘制的选择器来完成。由于有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);
        }
    });
}

全部完成!

【讨论】:

    【解决方案2】:

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

    在drawable中添加background_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item  android:drawable="@android:color/black" android:state_pressed="true"/>
    </selector>
    

    在适配器布局中使用上面的 xml,如下所示:

    【讨论】:

      【解决方案3】:

      尝试将此属性添加到回收站视图的 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>
      

      【讨论】:

      • 为什么设置背景颜色对我有帮助?您发布的链接与我在最初问题中发布的链接完全相同。
      • 哦,好吧,对不起,我的错我刚醒来:p 请尝试将属性添加到您的回收站视图中,如果它有效,请告诉我
      • 没问题。可悲的是它不起作用。它只会让我所有的物品变灰。
      • 你在使用 cardView 吗?
      • 并尝试使用我上面给你的属性添加这个属性 android:clickable="true" 和 android:focusable="true"
      猜你喜欢
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 2021-08-22
      • 2020-10-22
      • 1970-01-01
      • 2017-04-01
      相关资源
      最近更新 更多