【问题标题】:RecyclerView item touch to highlightRecyclerView 项目触摸以突出显示
【发布时间】:2015-10-25 02:42:14
【问题描述】:

我正在使用RecyclerView,当我触摸RecyclerView 的项目时,我看不到任何反馈。如何实现?

我试图在用户触摸RecyclerView 行时向他们显示反馈。有点像涟漪效应。

我想知道如何在RecyclerView的特定行中实现它。

这是回收站视图的自定义行布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/drawer_row"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_toEndOf="@+id/navigation_image"
        android:layout_toRightOf="@+id/navigation_image"
        android:textSize="17sp" />

    <ImageView
        android:id="@+id/navigation_image"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp" />

    <TextView
        android:id="@+id/horizontal_line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="#F1F1F1" />


</RelativeLayout>

这是Activity 的布局,具有RecyclerView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/windowBackground">


    <RelativeLayout
        android:id="@+id/nav_header_container"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/ic_drawer_header">

        <ImageView
            android:id="@+id/circle_imageview"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/profile_pic"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="John Doe"
            android:textSize="20sp"
            android:textColor="#ffffff"
            android:layout_below="@+id/circle_imageview"
            android:layout_alignLeft="@+id/circle_imageview"
            android:layout_alignStart="@+id/circle_imageview"
            android:layout_marginTop="5dp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Transaction Technologies"
            android:layout_below="@+id/textView"
            android:layout_alignLeft="@+id/textView"
            android:layout_alignStart="@+id/textView"
            android:textColor="#ffffff"
            android:layout_marginTop="5dp" />

    </RelativeLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/drawerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/nav_header_container" />


</RelativeLayout>

我只需要突出显示/提供用户正在触摸的行的反馈。我如何实现它?

提前致谢。

【问题讨论】:

  • RecyclerView onClick的可能重复
  • @SatyenUdeshi:甚至没有关闭
  • 删除这个 android:background="@color/windowBackground"
  • 那个背景是 FragmentDrawers 的整个背景。
  • 你的 min-sdk 版本是多少? @Shaikh MD Ashiq

标签: android material-design android-recyclerview


【解决方案1】:

Lollipop 之前的设备不支持波纹效果。

还是用这个material-ripple

compile 'com.balysv:material-ripple:1.0.2'

在 holder.drawView 上应用波纹

MaterialRippleLayout.on(holder.drawerView)
           .rippleColor(Color.RED)
           .create();

或者你可以从xml申请

<com.balysv.materialripple.MaterialRippleLayout
    android:id="@+id/ripple"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button inside a ripple"/>

</com.balysv.materialripple.MaterialRippleLayout>

【讨论】:

  • 我做了这个 holder.drawerView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MaterialRippleLayout.on(holder.drawerView) .rippleColor(Color.RED) 。创建();}});
  • 然后,它正在调整视图的大小以包装内容。
  • 查看库中的演示项目。他在 Recycler 行项目上应用了涟漪效应。而且不像你。见this
【解决方案2】:

我可以帮助你,但我也需要你的一些反馈。在 recycler View adapter 内的 onCreateVIewHolder 中,您必须定义 textViews 并将其分配给示例回收器视图中的元素。添加这个。确保在函数外部定义布局,以便在整个类中访问它

RelativeLayout layout;

layout=(RelativeLayout)view.findViewById.(R.layout.drawer_row)

然后转到你的回收器视图适配器并在onBindViewHolder 中定义

holder.layout.setOnClickListener(new View.OnClickListener() {

 @Override

 public void onClick(View v) {

 // Handle onClick event

     }
  });

在 onClick 事件中,您可以添加您希望用户体验的任何类型的反馈,例如开始新活动、祝酒词、消息或在这种情况下产生连锁反应。您可以使用自定义库来处理诸如此类的涟漪

Ripple effect for lollipop views

https://android-arsenal.com/details/1/980

https://github.com/balysv/material-ripple

【讨论】:

  • 谢谢。你很亲密。实际上,我们应该使用 onTouch 监听器。
  • 确实如此。无论哪种方式,实现它的格式都是相同的。如果你想为每个 recyclerview 行中的单个图像或文本添加触摸事件,请以相同的方式定义它们的 onClick / onTouch 侦听器。
【解决方案3】:

您可以使用THIS LIBRARY 添加波纹效果。使用将其包含在您的 Gradle 文件中。

compile 'com.thomsonreuters:rippledecoratorview:+'

只需将您的行布局放在 RippleDecoratorView 中。例如:

<com.thomsonreuters.rippledecoratorview.RippleDecoratorView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_margin="4dp"
  rdv:rdv_rippleColor="@android:color/holo_blue_dark"
  rdv:rdv_rippleAnimationFrames="60"
  rdv:rdv_rippleAnimationPeakFrame="15"
  rdv:rdv_rippleMaxAlpha="0.8"
  rdv:rdv_rippleAnimationDuration="600"
  rdv:rdv_rippleRadius="50dp">



  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/drawer_row"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_toEndOf="@+id/navigation_image"
        android:layout_toRightOf="@+id/navigation_image"
        android:textSize="17sp" />

    <ImageView
        android:id="@+id/navigation_image"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp" />

    <TextView
        android:id="@+id/horizontal_line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="#F1F1F1" />


</RelativeLayout>

</com.thomsonreuters.rippledecoratorview.RippleDecoratorView>

或者您可以根据您的要求使用这些库中的任何一个:

1>https://github.com/balysv/material-ripple

2>https://github.com/siriscac/RippleView

3>https://github.com/traex/RippleEffect

【讨论】:

  • 我只是希望用户在接触回收站时获得一些反馈。这不起作用。
  • 有点像涟漪效应,点击recyclerview的一行。
  • 我有问题,把它放在我的行布局中。你能告诉我怎么做吗?
  • @ShaikMDAshiq 在这里看看是否有帮助。
  • @ShaikMDAshiq 您是否在视图中添加了点击监听器并查看是否实际收到了点击?
【解决方案4】:

您需要在RecyclerView 的自定义行项中添加android:focusable="true"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/drawer_row"
    android:background="?android:selectableItemBackground"
    android:focusable="true"
    android:clickable="true" >
</RelativeLayout>

【讨论】:

  • 你改变背景属性了吗?
  • 对不起,你什么意思?
  • Recylcer View 需要添加android:focusable="true" 用于重点关注特定视图,android:clickable="true" 用于允许点击点击行。背景效果是由android:background="?android:selectableItemBackground" 在行父视图上实现的..
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2014-12-21
  • 1970-01-01
  • 2014-01-14
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
相关资源
最近更新 更多