recyclerViewAndroid开发Android技术知识

给RecyclerView封装个Adapter吧(更优雅的添加点

2016-08-12  本文已影响1676人  A_si

RecyclerView强大,好用,但是使用率很高的ItemClickListener却没有添加。

点击事件的实现,有常见的三种方法:

从以上三种方式的实现过程可知:
三种均可实现 ItemView的点击事件和长按事件的监听.

OnItemTouchListener 源码:
public static interface OnItemTouchListener { public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e); public void onTouchEvent(RecyclerView rv, MotionEvent e); public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept); }
此接口还提供了一个实现类,且官方推荐使用该实现类 SimpleOnItemTouchListener:

/**
 * An implementation of {@link RecyclerView.OnItemTouchListener} that has empty method bodies and
 * default return values.
 * <p>
 * You may prefer to extend this class if you don't need to override all methods. Another
 * benefit of using this class is future compatibility. As the interface may change, we'll
 * always provide a default implementation on this class so that your code won't break when
 * you update to a new version of the support library.
 */
public static class SimpleOnItemTouchListener implements RecyclerView.OnItemTouchListener {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
}

在触摸接口中,当触摸时会回调一个 MotionEvent对象,看到MotionEvent然后想到了什么?通过使用 GestureDetectorCompat来解析用户的操作!GestureDetectorCompat 就是处理手势的类:手势探测器,它比GestureDetector能更好兼容低版本的api,但使用方法是一致的。而GestureDetectorCompat还提供了一个外部类SimpleOnGestureListener,这个类实现了上面GestureDetectorCompat接口的所有方法,但全都是空实现,这样继承SimpleOnGestureListener的时候就不用实现每一个方法了,我们需要点击和长按,只需要实现:

boolean onSingleTapUp(MotionEvent e)
void onLongPress(MotionEvent e)

然后根据点击的坐标得到viewhoder:

View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (childView != null) {
                RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(childView);
                onItemClick(vh);//点击回掉
            }

全部代码:

public abstract class OnRecyclerItemClickListener extends RecyclerView.SimpleOnItemTouchListener
{
private GestureDetectorCompat mGestureDetector;
private RecyclerView recyclerView;

public OnRecyclerItemClickListener(final RecyclerView recyclerView)
{
    this.recyclerView = recyclerView;
    mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new GestureDetector.SimpleOnGestureListener()
    {
        @Override
        public boolean onSingleTapUp(MotionEvent e)
        {
            View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (childView != null)
            {
                RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(childView);
                onItemClick(vh);
            }
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e)
        {
            View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (childView != null)
            {
                RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(childView);
                onItemLongClick(vh);
            }
        }
    });
}
 //点击事件交给mGestureDetector处理
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e)
{
    mGestureDetector.onTouchEvent(e);
    return false;
}
    //点击回掉
public abstract void onItemClick(RecyclerView.ViewHolder vh);
    //长按监听
public abstract void onItemLongClick(RecyclerView.ViewHolder vh);
}

使用:

    recyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(recyclerView)
    {
        @Override
        public void onItemClick(RecyclerView.ViewHolder vh)
        {
            int adapterPosition = vh.getAdapterPosition();//当前item的位置
            Log.i(TAG, "onItemClick: "+adapterPosition);
        }

        @Override
        public void onItemLongClick(RecyclerView.ViewHolder vh)
        {
            Log.i(TAG, "onItemLongClick: ");
        }
    });

传送门

上一篇下一篇

猜你喜欢

热点阅读