分享一个RecyclerView的click技巧

作者: HitenDev | 来源:发表于2016-11-27 18:14 被阅读407次

    如何处理一个RecyclerView列表点击事件,在网上一般能搜到两种方案:

    • 从ViewHolder中得到rootView,然后进行setOnClickListener();

    • 实现RecyclerView.OnItemTouchListener接口,从Touch事件中分发出Click事件;

    这两种解决方案在网上都有讲解,想了解更多的,可以参考这篇博客,我在实际开发中,更倾向于第一种方案,因为这种方案比较简单,但是每次还得在ViewHoler中写代码,很多时候点击事件的逻辑应该在activity中实现,这样还得定义用于回调的接口,然后从activity得到viewHoler对象,set事件,作为一个有洁癖的程序员,这是多么邋遢的写法,但更可耻的是,我竟然一直在默默接受了;

    今天在闲逛国外大牛博客时,发现一种封装的技巧,也是基于view的setOnClickListener方法,但不是在viewHolder获取view;

    先看看怎么调用:

    ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
        @Override
        public void onItemClicked(RecyclerView recyclerView, int position, View v) {
            // do it
        }
    });
    

    ItemClickSupport源码也很简单:

    public class ItemClickSupport {
        private final RecyclerView mRecyclerView;
        private OnItemClickListener mOnItemClickListener;
        private OnItemLongClickListener mOnItemLongClickListener;
        private View.OnClickListener mOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                    mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
                }
            }
        };
        private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemLongClickListener != null) {
                    RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                    return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
                }
                return false;
            }
        };
        private RecyclerView.OnChildAttachStateChangeListener mAttachListener
                = new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {
                if (mOnItemClickListener != null) {
                    view.setOnClickListener(mOnClickListener);
                }
                if (mOnItemLongClickListener != null) {
                    view.setOnLongClickListener(mOnLongClickListener);
                }
            }
    
            @Override
            public void onChildViewDetachedFromWindow(View view) {
    
            }
        };
    
        private ItemClickSupport(RecyclerView recyclerView) {
            mRecyclerView = recyclerView;
            mRecyclerView.setTag(R.id.item_click_support, this);
            mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
        }
    
        public static ItemClickSupport addTo(RecyclerView view) {
            ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
            if (support == null) {
                support = new ItemClickSupport(view);
            }
            return support;
        }
    
        public static ItemClickSupport removeFrom(RecyclerView view) {
            ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
            if (support != null) {
                support.detach(view);
            }
            return support;
        }
    
        public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
            mOnItemClickListener = listener;
            return this;
        }
    
        public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
            mOnItemLongClickListener = listener;
            return this;
        }
    
        private void detach(RecyclerView view) {
            view.removeOnChildAttachStateChangeListener(mAttachListener);
            view.setTag(R.id.item_click_support, null);
        }
    
        public interface OnItemClickListener {
    
            void onItemClicked(RecyclerView recyclerView, int position, View v);
        }
    
        public interface OnItemLongClickListener {
    
            boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
        }
    }
    

    同时,你还需要在ids.xml中定义一个名字为item_click_support的id:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item name="item_click_support" type="id" />
    </resources>
    
    

    或者,你可以把R.id.item_click_support换成一个自己明白的int数值,这样就不用定义id了;

    注意:请在不需要监听时或者activity销毁前,remove掉callBack

    感谢代码出处作者,感谢大家的阅读,如果使用中有什么bug,欢迎一起探讨!

    代码出处:http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks

    相关文章

      网友评论

        本文标题:分享一个RecyclerView的click技巧

        本文链接:https://www.haomeiwen.com/subject/ckjnpttx.html