RecyclerView 实现点击的另外一种方式
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



阅读更多
个人分类: Android java web
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

RecyclerView 实现点击的另外一种方式

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭