ListView和RecyclerView列表点击反馈

来源:互联网 发布:php log日志类 编辑:程序博客网 时间:2024/04/20 03:58

整理下Android开发中列表点击反馈的一些知识点,就是点击Item会出现背景阴影的效果。

目前最常用的两个列表控件ListView和RecyclerView,可以说RecyclerView作为ListView的升级版有着更强大的功能,现在也基本都使用RecyclerView居多。当然了,RecyclerView还是有些属性设置没有但ListView有,就是下面要说的点击反馈。

ListView

先来说说ListView,ListView默认就带有点击反馈效果,不需要特意去设置。当然了,你如果不想要默认的点击反馈效果或想替换别的样式,都可以通过设置一下属性来实现:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 去除ListView的点击反馈效果 -->  
  2. <ListView  
  3.     android:listSelector="@android:color/transparent"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"/>  
  6.   
  7. <!-- 自定义ListView的点击反馈效果 -->  
  8. <ListView  
  9.     android:id="@+id/lv_list"  
  10.     android:listSelector="@drawable/sel_common_bg_press"  
  11.     android:layout_width="match_parent"  
  12.     android:layout_height="match_parent"/>  

其中 sel_common_bg_press.xml 文件如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:state_pressed="true">  
  5.         <shape>  
  6.             <solid android:color="#88ff5722"/>  
  7.         </shape>  
  8.     </item>  
  9.     <item android:drawable="@android:color/transparent"/>  
  10. </selector>  

这个很简单,就是定义了按压和没按压时的颜色状态。

基本上ListView的点击反馈都是通过这个属性来处理,不过在一些特殊情况下会出现点击反馈无效的情况:

  1. 设置了Item布局的android:background属性,并且设置为不透明的背景色,在这种情况下就会看不到点击反馈效果,但实际上android:listSelector属性还是处于作用的状态,只不过被覆盖了看不到。你可以用半透明的背景色设置android:background属性,这种情况还是能看到点击反馈的;
  2. Item布局里的子视图占满了整个布局,比如ImageView的图片占满整个Item,这时也看不到点击反馈;
  3. 还有一个不算点击反馈无效的情况就是,如果Item里面的控件接收并处理了点击事件,点击反馈效果也看不到,因为点击事件被别人消耗了;

其实上面前两情况类似,都是点击效果被覆盖了,因为默认的点击反馈响应的是Item的背景而不是前景,这个和后面要提到的android:foreground相对应。不过ListView还有一个属性来处理这种情况:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- android:drawSelectorOnTop="true"让点击反馈为Item的最顶部 -->  
  2. <ListView  
  3.     android:id="@+id/lv_list"  
  4.     android:drawSelectorOnTop="true"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"/>  

设置了android:drawSelectorOnTop="true"属性后,即便Item布局被其它东西覆盖了也能看到点击反馈效果。

RecyclerView

RecyclerView就没有ListView这么方便了,默认是没有点击反馈的,需要我们自己设置。简单的话可以设置Item布局的android:background属性,这个效果就和ListView的android:listSelector属性效果类似,但和ListView还是会面临同样的点击看不到效果的情况,而且RecyclerView并没有android:drawSelectorOnTop这些相关的属性,所以要另外想办法处理。
这个时候就要用前面提到的android:foreground设置前景色属性啦,不过这个属性只有在android 6.0版本以上或者FrameLayout控件布局上使用才有效果,所以为了兼容低版本,只能选择使用FrameLayout布局来作为Item的顶层布局(该方法同样适用ListView)。使用如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:clickable="true"  
  7.     android:foreground="@drawable/sel_common_bg_press">  
  8.   
  9.     <ImageView  
  10.         android:id="@+id/iv_icon"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_gravity="center"  
  14.         android:scaleType="centerCrop"  
  15.         android:src="@mipmap/ic_pic"/>  
  16. </FrameLayout>  

实际也挺简单的,就是让FrameLayout作为最外层布局就行了。有一点需要注意下,我对FrameLayout设置了android:clickable="true"属性,这么做是为了说明FrameLayout要能接收到点击事件才会有点击反馈效果!正常情况我们并不需要在这里设置,因为我们通常会给列表的Item添加点击监听,这样也能看到点击反馈效果。如果你要使用系统默认的点击反馈样式,可以用这个android:foreground="?android:attr/selectableItemBackground"。
到此RecyclerView的点击反馈也能够处理了,下面在讲下自定义布局的点击反馈效果,就拿经典的开源项目RippleEffect来介绍,这个同样适用ListView。

RippleView

注意,我对RippleEffect源码做了些修改,来让效果更贴近平时的使用。我直接贴源码,因为这里主要讲点击反馈,我只在源码的基础上增加了些触摸的判断。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class RippleView extends RelativeLayout {  
  2.   
  3.     // 点击加速度  
  4.     private static final int RIPPLE_ACCELERATE = 20;  
  5.     // 5种触摸状态  
  6.     private static final int RIPPLE_NORMAL = 0;  
  7.     private static final int RIPPLE_SINGLE = 1;  
  8.     private static final int RIPPLE_LONG_PRESS = 2;  
  9.     private static final int RIPPLE_ACTION_MOVE = 3;  
  10.     private static final int RIPPLE_ACTION_UP = 4;  
  11.     // 触摸状态  
  12.     private int rippleStatus = RIPPLE_NORMAL;  
  13.     // 是否为列表模式,默认为否  
  14.     private boolean isListMode;  
  15.     // 用户是否滑动的判别距离  
  16.     private int touchSlop;  
  17.     // 长按时的坐标  
  18.     private int lastLongPressX;  
  19.     private int lastLongPressY;  
  20.   
  21.     private int WIDTH;  
  22.     private int HEIGHT;  
  23.     private int frameRate = 10;  
  24.     private int rippleDuration = 400;  
  25.     private int rippleAlpha = 90;  
  26.     private Handler canvasHandler;  
  27.     private float radiusMax = 0;  
  28.     private boolean animationRunning = false;  
  29.     private int timer = 0;  
  30.     private int timerEmpty = 0;  
  31.     private int durationEmpty = -1;  
  32.     private float x = -1;  
  33.     private float y = -1;  
  34.     private int zoomDuration;  
  35.     private float zoomScale;  
  36.     private ScaleAnimation scaleAnimation;  
  37.     private Boolean hasToZoom;  
  38.     private Boolean isCentered;  
  39.     private Integer rippleType;  
  40.     private Paint paint;  
  41.     private Bitmap originBitmap;  
  42.     private int rippleColor;  
  43.     private int ripplePadding;  
  44.     private GestureDetector gestureDetector;  
  45.     private final Runnable runnable = new Runnable() {  
  46.         @Override  
  47.         public void run() {  
  48.             invalidate();  
  49.         }  
  50.     };  
  51.   
  52.     private OnRippleCompleteListener onCompletionListener;  
  53.   
  54.     public RippleView(Context context) {  
  55.         super(context);  
  56.     }  
  57.   
  58.     public RippleView(Context context, AttributeSet attrs) {  
  59.         super(context, attrs);  
  60.         init(context, attrs);  
  61.     }  
  62.   
  63.     public RippleView(Context context, AttributeSet attrs, int defStyle) {  
  64.         super(context, attrs, defStyle);  
  65.         init(context, attrs);  
  66.     }  
  67.   
  68.     /** 
  69.      * Method that initializes all fields and sets listeners 
  70.      * 
  71.      * @param context Context used to create this view 
  72.      * @param attrs   Attribute used to initialize fields 
  73.      */  
  74.     private void init(final Context context, final AttributeSet attrs) {  
  75.         if (isInEditMode())  
  76.             return;  
  77.   
  78.         final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RippleView);  
  79.         rippleColor = typedArray.getColor(R.styleable.RippleView_rv_color, Color.parseColor("#33626262"));  
  80.         rippleType = typedArray.getInt(R.styleable.RippleView_rv_type, 0);  
  81.         hasToZoom = typedArray.getBoolean(R.styleable.RippleView_rv_zoom, false);  
  82.         isCentered = typedArray.getBoolean(R.styleable.RippleView_rv_centered, false);  
  83.         rippleDuration = typedArray.getInteger(R.styleable.RippleView_rv_rippleDuration, rippleDuration);  
  84.         frameRate = typedArray.getInteger(R.styleable.RippleView_rv_framerate, frameRate);  
  85.         rippleAlpha = typedArray.getInteger(R.styleable.RippleView_rv_alpha, rippleAlpha);  
  86.         ripplePadding = typedArray.getDimensionPixelSize(R.styleable.RippleView_rv_ripplePadding, 0);  
  87.         canvasHandler = new Handler();  
  88.         zoomScale = typedArray.getFloat(R.styleable.RippleView_rv_zoomScale, 1.03f);  
  89.         zoomDuration = typedArray.getInt(R.styleable.RippleView_rv_zoomDuration, 200);  
  90.         isListMode = typedArray.getBoolean(R.styleable.RippleView_rv_listMode, false);  
  91.         typedArray.recycle();  
  92.         paint = new Paint();  
  93.         paint.setAntiAlias(true);  
  94.         paint.setStyle(Paint.Style.FILL);  
  95.         paint.setColor(rippleColor);  
  96.         paint.setAlpha(rippleAlpha);  
  97.         this.setWillNotDraw(false);  
  98.   
  99.         gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {  
  100.             @Override  
  101.             public void onLongPress(MotionEvent event) {  
  102.                 super.onLongPress(event);  
  103.                 animateRipple(event);  
  104.                 sendClickEvent(true);  
  105.                 lastLongPressX = (int) event.getX();  
  106.                 lastLongPressY = (int) event.getY();  
  107.                 rippleStatus = RIPPLE_LONG_PRESS;  
  108.             }  
  109.   
  110.             @Override  
  111.             public boolean onSingleTapConfirmed(MotionEvent e) {  
  112.                 return true;  
  113.             }  
  114.   
  115.             @Override  
  116.             public boolean onSingleTapUp(MotionEvent e) {  
  117.                 return true;  
  118.             }  
  119.         });  
  120.   
  121.         this.setDrawingCacheEnabled(true);  
  122.         this.setClickable(true);  
  123.         this.touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();  
  124.     }  
  125.   
  126.     @Override  
  127.     public void draw(Canvas canvas) {  
  128.         super.draw(canvas);  
  129.         if (animationRunning) {  
  130.             if (isListMode && (rippleStatus == RIPPLE_SINGLE  
  131.                     || rippleStatus == RIPPLE_ACTION_MOVE || rippleStatus == RIPPLE_ACTION_UP)) {  
  132.                 doRippleWork(canvas, RIPPLE_ACCELERATE);  
  133.             } else {  
  134.                 doRippleWork(canvas, 1);  
  135.             }  
  136.         }  
  137.     }  
  138.   
  139.     private void doRippleWork(Canvas canvas, int offect) {  
  140.         canvas.save();  
  141.         if (rippleDuration <= timer * frameRate) {  
  142.             // There is problem on Android M where canvas.restore() seems to be called automatically  
  143.             // For now, don't call canvas.restore() manually on Android M (API 23)  
  144.             canvas.drawCircle(x, y, (radiusMax * (((float) timer * frameRate) / rippleDuration)), paint);  
  145.   
  146.             if (onCompletionListener != null && rippleStatus != RIPPLE_ACTION_MOVE && rippleStatus != RIPPLE_LONG_PRESS) {  
  147.                 onCompletionListener.onComplete(this);  
  148.             }  
  149.             if (rippleStatus != RIPPLE_LONG_PRESS) {  
  150.                 animationRunning = false;  
  151.                 rippleStatus = RIPPLE_NORMAL;  
  152.                 timer = 0;  
  153.                 durationEmpty = -1;  
  154.                 timerEmpty = 0;  
  155.                 if (Build.VERSION.SDK_INT != 23) {  
  156.                     canvas.restore();  
  157.                 }  
  158.             }  
  159.             invalidate();  
  160.             return;  
  161.         } else  
  162.             canvasHandler.postDelayed(runnable, frameRate);  
  163.   
  164.         if (timer == 0)  
  165.             canvas.save();  
  166.   
  167.         canvas.drawCircle(x, y, (radiusMax * (((float) timer * frameRate) / rippleDuration)), paint);  
  168.   
  169.         paint.setColor(Color.parseColor("#ffff4444"));  
  170.   
  171.         if (rippleType == 1 && originBitmap != null && (((float) timer * frameRate) / rippleDuration) > 0.4f) {  
  172.             if (durationEmpty == -1)  
  173.                 durationEmpty = rippleDuration - timer * frameRate;  
  174.   
  175.             timerEmpty++;  
  176.             final Bitmap tmpBitmap = getCircleBitmap((int) ((radiusMax) * (((float) timerEmpty * frameRate) / (durationEmpty))));  
  177.             canvas.drawBitmap(tmpBitmap, 00, paint);  
  178.             tmpBitmap.recycle();  
  179.         }  
  180.   
  181.         paint.setColor(rippleColor);  
  182.         if (!isListMode) {  
  183.             if (rippleType == 1) {  
  184.                 if ((((float) timer * frameRate) / rippleDuration) > 0.6f)  
  185.                     paint.setAlpha((int) (rippleAlpha - ((rippleAlpha) * (((float) timerEmpty * frameRate) / (durationEmpty)))));  
  186.                 else  
  187.                     paint.setAlpha(rippleAlpha);  
  188.             } else  
  189.                 paint.setAlpha((int) (rippleAlpha - ((rippleAlpha) * (((float) timer * frameRate) / rippleDuration))));  
  190.         }  
  191.         timer += offect;  
  192.     }  
  193.   
  194.     @Override  
  195.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  196.         super.onSizeChanged(w, h, oldw, oldh);  
  197.         WIDTH = w;  
  198.         HEIGHT = h;  
  199.   
  200.         scaleAnimation = new ScaleAnimation(1.0f, zoomScale, 1.0f, zoomScale, w / 2, h / 2);  
  201.         scaleAnimation.setDuration(zoomDuration);  
  202.         scaleAnimation.setRepeatMode(Animation.REVERSE);  
  203.         scaleAnimation.setRepeatCount(1);  
  204.     }  
  205.   
  206.     /** 
  207.      * Launch Ripple animation for the current view with a MotionEvent 
  208.      * 
  209.      * @param event MotionEvent registered by the Ripple gesture listener 
  210.      */  
  211.     public void animateRipple(MotionEvent event) {  
  212.         createAnimation(event.getX(), event.getY());  
  213.     }  
  214.   
  215.     /** 
  216.      * Launch Ripple animation for the current view centered at x and y position 
  217.      * 
  218.      * @param x Horizontal position of the ripple center 
  219.      * @param y Vertical position of the ripple center 
  220.      */  
  221.     public void animateRipple(final float x, final float y) {  
  222.         createAnimation(x, y);  
  223.     }  
  224.   
  225.     /** 
  226.      * Create Ripple animation centered at x, y 
  227.      * 
  228.      * @param x Horizontal position of the ripple center 
  229.      * @param y Vertical position of the ripple center 
  230.      */  
  231.     private void createAnimation(final float x, final float y) {  
  232.         if (this.isEnabled() && !animationRunning) {  
  233.             if (hasToZoom)  
  234.                 this.startAnimation(scaleAnimation);  
  235.   
  236.             radiusMax = Math.max(WIDTH, HEIGHT);  
  237.   
  238.             if (rippleType != 2)  
  239.                 radiusMax /= 2;  
  240.   
  241.             radiusMax -= ripplePadding;  
  242.   
  243.             if (isCentered || rippleType == 1) {  
  244.                 this.x = getMeasuredWidth() / 2;  
  245.                 this.y = getMeasuredHeight() / 2;  
  246.             } else {  
  247.                 this.x = x;  
  248.                 this.y = y;  
  249.             }  
  250.   
  251.             animationRunning = true;  
  252.   
  253.             if (rippleType == 1 && originBitmap == null)  
  254.                 originBitmap = getDrawingCache(true);  
  255.   
  256.             invalidate();  
  257.         }  
  258.     }  
  259.   
  260.     @Override  
  261.     public boolean onTouchEvent(MotionEvent event) {  
  262.         if (gestureDetector.onTouchEvent(event)) {  
  263.             animateRipple(event);  
  264.             sendClickEvent(false);  
  265.             rippleStatus = RIPPLE_SINGLE;  
  266.         }  
  267.         if (rippleStatus == RIPPLE_LONG_PRESS) {  
  268.             if (event.getAction() == MotionEvent.ACTION_UP) {  
  269.                 rippleStatus = RIPPLE_ACTION_UP;  
  270.             } else if (Math.abs(event.getX() - lastLongPressX) >= touchSlop ||  
  271.                     Math.abs(event.getY() - lastLongPressY) >= touchSlop) {  
  272.                 rippleStatus = RIPPLE_ACTION_MOVE;  
  273.             }  
  274.         }  
  275.   
  276.         return super.onTouchEvent(event);  
  277.     }  
  278.   
  279.     @Override  
  280.     public boolean onInterceptTouchEvent(MotionEvent event) {  
  281.         this.onTouchEvent(event);  
  282.         return super.onInterceptTouchEvent(event);  
  283.     }  
  284.   
  285.     /** 
  286.      * Send a click event if parent view is a Listview instance 
  287.      * 
  288.      * @param isLongClick Is the event a long click ? 
  289.      */  
  290.     private void sendClickEvent(final Boolean isLongClick) {  
  291.         if (getParent() instanceof AdapterView) {  
  292.             final AdapterView adapterView = (AdapterView) getParent();  
  293.             final int position = adapterView.getPositionForView(this);  
  294.             final long id = adapterView.getItemIdAtPosition(position);  
  295.             if (isLongClick) {  
  296.                 if (adapterView.getOnItemLongClickListener() != null)  
  297.                     adapterView.getOnItemLongClickListener().onItemLongClick(adapterView, this, position, id);  
  298.             } else {  
  299.                 if (adapterView.getOnItemClickListener() != null)  
  300.                     adapterView.getOnItemClickListener().onItemClick(adapterView, this, position, id);  
  301.             }  
  302.         }  
  303.     }  
  304.   
  305.     private Bitmap getCircleBitmap(final int radius) {  
  306.         final Bitmap output = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(), Bitmap.Config.ARGB_8888);  
  307.         final Canvas canvas = new Canvas(output);  
  308.         final Paint paint = new Paint();  
  309.         final Rect rect = new Rect((int) (x - radius), (int) (y - radius), (int) (x + radius), (int) (y + radius));  
  310.   
  311.         paint.setAntiAlias(true);  
  312.         canvas.drawARGB(0000);  
  313.         canvas.drawCircle(x, y, radius, paint);  
  314.   
  315.         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
  316.         canvas.drawBitmap(originBitmap, rect, rect, paint);  
  317.   
  318.         return output;  
  319.     }  
  320.   
  321.     /** 
  322.      * Set Ripple color, default is #FFFFFF 
  323.      * 
  324.      * @param rippleColor New color resource 
  325.      */  
  326.     public void setRippleColor(@ColorRes int rippleColor) {  
  327.         this.rippleColor = ContextCompat.getColor(getContext(), rippleColor);  
  328.     }  
  329.   
  330.     public int getRippleColor() {  
  331.         return rippleColor;  
  332.     }  
  333.   
  334.     public RippleType getRippleType() {  
  335.         return RippleType.values()[rippleType];  
  336.     }  
  337.   
  338.     /** 
  339.      * Set Ripple type, default is RippleType.SIMPLE 
  340.      * 
  341.      * @param rippleType New Ripple type for next animation 
  342.      */  
  343.     public void setRippleType(final RippleType rippleType) {  
  344.         this.rippleType = rippleType.ordinal();  
  345.     }  
  346.   
  347.     public Boolean isCentered() {  
  348.         return isCentered;  
  349.     }  
  350.   
  351.     /** 
  352.      * Set if ripple animation has to be centered in its parent view or not, default is False 
  353.      * 
  354.      * @param isCentered 
  355.      */  
  356.     public void setCentered(final Boolean isCentered) {  
  357.         this.isCentered = isCentered;  
  358.     }  
  359.   
  360.     public int getRipplePadding() {  
  361.         return ripplePadding;  
  362.     }  
  363.   
  364.     /** 
  365.      * Set Ripple padding if you want to avoid some graphic glitch 
  366.      * 
  367.      * @param ripplePadding New Ripple padding in pixel, default is 0px 
  368.      */  
  369.     public void setRipplePadding(int ripplePadding) {  
  370.         this.ripplePadding = ripplePadding;  
  371.     }  
  372.   
  373.     public Boolean isZooming() {  
  374.         return hasToZoom;  
  375.     }  
  376.   
  377.     /** 
  378.      * At the end of Ripple effect, the child views has to zoom 
  379.      * 
  380.      * @param hasToZoom Do the child views have to zoom ? default is False 
  381.      */  
  382.     public void setZooming(Boolean hasToZoom) {  
  383.         this.hasToZoom = hasToZoom;  
  384.     }  
  385.   
  386.     public float getZoomScale() {  
  387.         return zoomScale;  
  388.     }  
  389.   
  390.     /** 
  391.      * Scale of the end animation 
  392.      * 
  393.      * @param zoomScale Value of scale animation, default is 1.03f 
  394.      */  
  395.     public void setZoomScale(float zoomScale) {  
  396.         this.zoomScale = zoomScale;  
  397.     }  
  398.   
  399.     public int getZoomDuration() {  
  400.         return zoomDuration;  
  401.     }  
  402.   
  403.     /** 
  404.      * Duration of the ending animation in ms 
  405.      * 
  406.      * @param zoomDuration Duration, default is 200ms 
  407.      */  
  408.     public void setZoomDuration(int zoomDuration) {  
  409.         this.zoomDuration = zoomDuration;  
  410.     }  
  411.   
  412.     public int getRippleDuration() {  
  413.         return rippleDuration;  
  414.     }  
  415.   
  416.     /** 
  417.      * Duration of the Ripple animation in ms 
  418.      * 
  419.      * @param rippleDuration Duration, default is 400ms 
  420.      */  
  421.     public void setRippleDuration(int rippleDuration) {  
  422.         this.rippleDuration = rippleDuration;  
  423.     }  
  424.   
  425.     public int getFrameRate() {  
  426.         return frameRate;  
  427.     }  
  428.   
  429.     /** 
  430.      * Set framerate for Ripple animation 
  431.      * 
  432.      * @param frameRate New framerate value, default is 10 
  433.      */  
  434.     public void setFrameRate(int frameRate) {  
  435.         this.frameRate = frameRate;  
  436.     }  
  437.   
  438.     public int getRippleAlpha() {  
  439.         return rippleAlpha;  
  440.     }  
  441.   
  442.     /** 
  443.      * Set alpha for ripple effect color 
  444.      * 
  445.      * @param rippleAlpha Alpha value between 0 and 255, default is 90 
  446.      */  
  447.     public void setRippleAlpha(int rippleAlpha) {  
  448.         this.rippleAlpha = rippleAlpha;  
  449.     }  
  450.   
  451.     public void setOnRippleCompleteListener(OnRippleCompleteListener listener) {  
  452.         this.onCompletionListener = listener;  
  453.     }  
  454.   
  455.     /** 
  456.      * Defines a callback called at the end of the Ripple effect 
  457.      */  
  458.     public interface OnRippleCompleteListener {  
  459.         void onComplete(RippleView rippleView);  
  460.     }  
  461.   
  462.     public enum RippleType {  
  463.         SIMPLE(0),  
  464.         DOUBLE(1),  
  465.         RECTANGLE(2);  
  466.   
  467.         int type;  
  468.   
  469.         RippleType(int type) {  
  470.             this.type = type;  
  471.         }  
  472.     }  
  473. }  

使用也很简单,把RippleView作为最外层布局就行了。

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <com.dl7.listclickfeedback.RippleView  
  3.     android:id="@+id/item_ripple"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="wrap_content"  
  8.     app:rv_listMode="true"  
  9.     app:rv_color="#88ff5722"  
  10.     app:rv_rippleDuration="800"  
  11.     app:rv_type="rectangle">  
  12.   
  13.     <LinearLayout  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:orientation="horizontal">  
  17.   
  18.         <ImageView  
  19.             android:id="@+id/iv_icon"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_gravity="center"  
  23.             android:layout_margin="30dp"  
  24.             android:src="@mipmap/ic_face_funny"/>  
  25.   
  26.         <TextView  
  27.             android:id="@+id/tv_title"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_gravity="center"  
  31.             android:gravity="center"/>  
  32.     </LinearLayout>  
  33. </com.dl7.listclickfeedback.RippleView>  

最后是点击事件的处理,需要让RippleView来接收处理点击事件。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2. public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {  
  3.     // ......  
  4.     ((ViewHolder) holder).mItemRipple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {  
  5.         @Override  
  6.         public void onComplete(RippleView rippleView) {  
  7.             ToastUtils.showToast(mDatas.get(position));  
  8.         }  
  9.     });  
  10. }  

这样就OK了,最后贴点图来说明反馈效果。

背景色反馈:


前景色反馈:



波纹反馈:




例子代码:列表点击反馈

0 0
原创粉丝点击