[英]RecyclerView item touch to highlight
我正在使用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>
这是具有RecyclerView
的Activity
的布局
<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
自定义行项目中添加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>
您可以使用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>
或者,您可以根据需要使用以下任何一种acc:
1> https://github.com/balysv/material-ripple
我可以为您提供帮助,但是我也需要您的一些反馈。 在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事件中,您可以添加想要用户体验的任何类型的反馈,例如开始新活动,敬酒,消息或在这种情况下产生连锁反应。 您可以将自定义库用于此类波动
棒棒糖之前的设备不支持纹波效果。
还是用这种材料涟漪
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.