gpt4 book ai didi

Android Recyclerview : onTouchListener on recyclerview to change viewholder color onTouch

转载 作者:行者123 更新时间:2023-11-30 01:42:13 25 4
gpt4 key购买 nike

在 Facebook 中,如果您触摸 Activity 源中的某个项目,它会立即变暗以表明您的手指在上面。如果您随后用手指滚动提要,则取景器会再次变为白色。

您也可以在回收 View 中按住某个项目,然后 View 会变暗。如果您抬起手指,则表示您已“点击”该项目,Facebook 会向您显示有关您所点击内容的更多信息。

这似乎是一件很容易实现的事情,但我一直在努力实现它。

example

我认为 Facebook 使用了 onTouchListener 来实现效果......

我写了下面的代码,但它似乎使我的 recyclerview 的 itemView 变暗了橙色,但是当我拖动或抬起手指时,橙色并没有像我想要的那样变成透明色。

同样因为 recyclerview 正在“回收”/重用它的 View ,如果我触摸列表中的第一个项目并滚动,从 recyclerview 底部出现的项目之一也将获得橙色色调作为 View 现在已移出视线(我单击的第一个 View )现在已被重新用于底部的 View 。

这是我的代码:

((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 和这篇文章:Background Selector in RecyclerView Item只需将以下内容设置为您的背景:

android:background="?android:attr/selectableItemBackground"

请注意,我在下面有两个 relativeLayout,内容将进入内部 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>

关于Android Recyclerview : onTouchListener on recyclerview to change viewholder color onTouch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34335748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com