[英]Android studio OnTouchListener for RecyclerView item, change item color
[英]Android Recyclerview: onTouchListener on recyclerview to change viewholder color onTouch
在Facebook中,如果您触摸活动供稿中的某个项目,则该项目会立即变暗以表明您的手指在上面。 如果然后用手指滚动提要,则视口将再次变白。
您还可以在自己的回收站视图中按住某个项目,并且视图保持器将变暗。 如果您松开手指,则说明您已“单击”该项目,Facebook会向您显示有关单击内容的更多信息。
这似乎很容易实现,但是我一直在努力。
我认为Facebook使用了onTouchListener来实现效果...
我写了下面的代码,但似乎用橙色调使recyclerview的itemView变暗,但是当我拖动或抬起手指时,橙色并没有变成我想要的透明颜色。
同样因为recyclerview正在“回收” /重复使用其视图,所以如果我触摸列表中的第一项并滚动,则从recyclerview底部出现的其中一个项也将变为橙色,因为该视图现已移动视线(我单击的第一个视图)现在已被底部视图重用。
这是我的代码:
((ProfileFeedViewHolder) holder).itemView.setOnTouchListener(new View.OnTouchListener() {
public final static int FINGER_RELEASED = 0;
public final static int FINGER_TOUCHED = 1;
public final static int FINGER_DRAGGING = 2;
public final static int FINGER_UNDEFINED = 3;
private int fingerState = FINGER_RELEASED;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.e("motionEvent", "motion "+motionEvent.getAction());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
if (fingerState == FINGER_RELEASED) fingerState = FINGER_TOUCHED;
else fingerState = FINGER_UNDEFINED;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.deep_orange_500));
break;
case MotionEvent.ACTION_UP:
if(fingerState != FINGER_DRAGGING) {
fingerState = FINGER_RELEASED;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
// Your onClick codes
}
else if (fingerState == FINGER_DRAGGING) fingerState = FINGER_RELEASED;
else fingerState = FINGER_UNDEFINED;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
break;
case MotionEvent.ACTION_MOVE:
if (fingerState == FINGER_TOUCHED || fingerState == FINGER_DRAGGING) {
fingerState = FINGER_DRAGGING;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
}
else fingerState = FINGER_UNDEFINED;
break;
default:
fingerState = FINGER_UNDEFINED;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
}
return false;
}
});
如何实现这种简单效果,这种效果在当今大多数应用程序中似乎很流行?
我用谷歌搜索了一下,您实际上可以在xml上执行此操作。 感谢Lawrr和这篇文章: RecyclerView Item中的Background Selector只需将以下内容设置为背景:
android:background="?android:attr/selectableItemBackground"
注意,我下面有两个relativeLayouts,内容将放入内部的relativeLayout中。 原因是android:background设置itemView的背景色。 我的itemViews都有白色背景,因此我需要将外部relativeLayout设置为白色。 然后,当我触摸itemView时,将内部relativeLayout设置为?android:attr/selectableItemBackground
背景将导致灰色突出显示和android涟漪效果。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/container">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground">
//put in your content here for each itemView
</RelativeLayout>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.