RecyclerView上的涟漪效应在轻按时不起作用 [英] RIpple effect on RecyclerView not working on light tap
问题描述
我试图在RecyclerView上实现涟漪效应.这是我的布局:
I have tried to implement the ripple effect on my RecyclerView. Here's my layout for it :
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_marginTop="7dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="5dp"
android:clickable="true"
card_view:cardElevation="5dp"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
//some properties />
<TextView
//some properties />
<TextView
//some properties />
</LinearLayout>
</android.support.v7.widget.CardView>
为了实现onclick侦听器,我基本上在这里遵循了本教程: http://sapandiwakar.in/recycler-view-item-click-handler/
In order to implement the onclick listener I basically followed this tutorial here: http://sapandiwakar.in/recycler-view-item-click-handler/
问题在于波纹效应是由于线条而产生的:
The problem is that the ripple effect,generated thanks to the lines :
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
不能在轻(即快速)压力机上工作.当我快速点击屏幕时,将触发点击侦听器.这意味着已检测到触摸事件,但是没有显示波纹效应.如果要查看涟漪效应,必须在释放之前将屏幕上的压力保持更长的时间.
is not working on light (it's to say quick) presses. When I just quickly tap the screen, the click listener is triggered. It means that the touch event have been detected, however there is no ripple effect showing. If I want to see the ripple effect, I have to maintain for a bit longer the pressure on the screen before releasing.
是否有一种方法可以纠正这种现象,甚至在快速按下时也能显示出波纹效果?
Is there a way to correct this behaviour and show a ripple effect even for quick presses?
推荐答案
我遇到了类似的问题.我在RecyclerView上有一个ItemClickListener,在那个侦听器中,我正在推送一个新片段.通过反复试验,我注意到删除或清空项目Click侦听器将使高亮显示每次显示,即使轻按也是如此.
I faced a similar problem. I had an ItemClickListener on the RecyclerView, and in that listener I was pushing a new fragment. By trial and error I noticed that removing or emptying the item click listener would get the highlight to show every time, even on light taps.
解决方法是更改我的onItemClick方法:
The fix was to change my onItemClick method:
@Override
public void onItemClick(final RecyclerView parent, final View view, final int position, final long id)
{
ViewCompat.postOnAnimationDelayed(parent, new Runnable()
{
@Override
public void run()
{
// Your click code goes here
}
}, 50);
}
这会将您的点击动作推迟到下一个动画步骤之后的50毫秒,并为动画提供足够的时间来加载和运行,然后再执行点击动作.
This postpones your click action until 50ms after the next animation step and gives the animation enough time to load and run before your click action is performed.
这篇关于RecyclerView上的涟漪效应在轻按时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!