关闭

[置顶] RecyclerView 实现点击的另外一种方式

标签: RecyclerViewOnItemClickListener
363人阅读 评论(0) 收藏 举报
分类:
1.前言:之前一直采用流行的方式来给RecyclerView的item设置点击方法,类似这样
  1.    @Override
  2.     public ViewHolder onCreateViewHolder(ViewGroup viewGroup,  int viewType) {
  3.         View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
  4.         ViewHolder vh = new ViewHolder(view);
  5.         //将创建的View注册点击事件
  6.         view.setOnClickListener(this);
  7.         return vh;
  8.     }
  9. 现在通过另外一种方式,去实现这个功能,基本逻辑就是利用GestureDector的特性,封装一个公共的类实现点击效果,废话不多少,上代码。

package com.example.android.recyclerview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    protected OnItemClickListener listener;
    private View childView;
    private GestureDetector gestureDetector;
    private int childViewPosition;
    public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        this.gestureDetector = new GestureDetector(context, new GestureListener());
        this.listener = listener;
    }
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        childView = rv.findChildViewUnder(e.getX(), e.getY());
        childViewPosition = rv.getChildPosition(childView);
        return childView != null && gestureDetector.onTouchEvent(e);
    }
    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }
    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
    protected class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent event) {
            if (childView != null) {
                listener.onItemClick(childView, childViewPosition);
            }
            return true;
        }
        @Override
        public void onLongPress(MotionEvent event) {
            if (childView != null) {
                listener.onItemLongPress(childView, childViewPosition);
            }
        }
        @Override
        public boolean onDown(MotionEvent event) {
            // Best practice to always return true here.
            // http://developer.android.com/training/gestures/detector.html#detect
            return true;
        }
    }
    public interface OnItemClickListener{
        public void onItemClick(View childView, int position);
        public void onItemLongPress(View childView, int position);
    }
    public static abstract class SimpleOnItemClickListener implements OnItemClickListener {
        public void onItemClick(View childView, int position) {
            // Do nothing.
        }
        public void onItemLongPress(View childView, int position) {
            // Do nothing.
        }
    }

}

然后在调用的时候要这么些写,
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getContext(),new OnItemClickListener()));
 private class OnItemClickListener extends RecyclerItemClickListener.SimpleOnItemClickListener {
        @Override
        public void onItemClick(View childView, int position) {
            Toast.makeText(getContext(),"点击第几个Item:" + position,Toast.LENGTH_SHORT).show();
        }
    }

个人感觉这种方式好在于封装,模拟了listView的点击方式,能拿到postion



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6102次
    • 积分:204
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论