简体   繁体   English

Android Recyclerview:recyclerview上的onTouchListener可以更改onTouch的视点颜色

[英]Android Recyclerview: onTouchListener on recyclerview to change viewholder color onTouch

In Facebook, if you touch an item in your activity feed, it instantly darkens to show that your finger is on it. 在Facebook中,如果您触摸活动供稿中的某个项目,则该项目会立即变暗以表明您的手指在上面。 If you then use your finger to scroll the feed, the viewholder becomes white again. 如果然后用手指滚动提要,则视口将再次变白。

You could also hold onto an item in your recyclerview and the viewholder will be darken. 您还可以在自己的回收站视图中按住某个项目,并且视图保持器将变暗。 If you lift your finger, you have 'clicked' the item and Facebook shows you more information about what you have clicked. 如果您松开手指,则说明您已“单击”该项目,Facebook会向您显示有关单击内容的更多信息。

This seems like quite an easy thing to implement but I have been struggling with it. 这似乎很容易实现,但是我一直在努力。

例

I think Facebook used the onTouchListener to implement the effect ... 我认为Facebook使用了onTouchListener来实现效果...

I wrote the code below but it seems to darken the itemView of my recyclerview with an orange tint but when i drag or lift up my finger, the orange does not become the transparent color as I want it to be. 我写了下面的代码,但似乎用橙色调使recyclerview的itemView变暗,但是当我拖动或抬起手指时,橙色并没有变成我想要的透明颜色。

Also because the recyclerview is 'recycling' / reusing its view, if I touch the first item in the list and scroll, one of the items coming up from the bottom of the recyclerview will also get the orange tint as the view that has now moved out of sight (the first view that I clicked) has now been reused for the view at the bottom. 同样因为recyclerview正在“回收” /重复使用其视图,所以如果我触摸列表中的第一项并滚动,则从recyclerview底部出现的其中一个项也将变为橙色,因为该视图现已移动视线(我单击的第一个视图)现在已被底部视图重用。

This is my code: 这是我的代码:

((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;
}
});

How do I implement this simple effect that seems to be very popular on most apps nowadays? 如何实现这种简单效果,这种效果在当今大多数应用程序中似乎很流行?

I google around a bit and you can actually do this on xml. 我用谷歌搜索了一下,您实际上可以在xml上执行此操作。 Thanks to Lawrr and this post: Background Selector in RecyclerView Item just set the following as your background: 感谢Lawrr和这篇文章: RecyclerView Item中的Background Selector只需将以下内容设置为背景:

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

Notice that I have two relativeLayouts below and the content would go into the inner relativeLayout. 注意,我下面有两个relativeLayouts,内容将放入内部的relativeLayout中。 The reason is that android:background sets the background color of the itemView. 原因是android:background设置itemView的背景色。 My itemViews all have white backgrounds, so I need to set the outer relativeLayout to white. 我的itemViews都有白色背景,因此我需要将外部relativeLayout设置为白色。 Then when I touch the itemView, the inner relativeLayout set to background of ?android:attr/selectableItemBackground will cause the gray highlight and the android ripple effect. 然后,当我触摸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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM