1. 程式人生 > >為RecyclerView新增優雅的點選事件、長按事件

為RecyclerView新增優雅的點選事件、長按事件

本篇參考:Hugo的文章-Getting your clicks on RecyclerView

效果如下


這裡寫圖片描述

一、完整的點選、長按事件支援類

  1. 先要準備一份resources
    res -> values -> ids.xml ->

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item name="item_click_support" type="id" />
    </resources>
    // 2. 具體的支援類
    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); } }

二、使用方式

可在setAdapter()之後呼叫

// 點選
ItemClickSupport.addTo(rv).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
    @Override public void onItemClicked(RecyclerView recyclerView, int position, View v) {
        Toast.makeText(MainActivity.this, mDatas.get(position), Toast.LENGTH_SHORT).show();
    }
});

// 長按
ItemClickSupport.addTo(rv).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
    @Override public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {

        Toast.makeText(MainActivity.this, "長按" + mDatas.get(position) + "已刪除", Toast.LENGTH_SHORT).show();

        // 需要自己處理position在集合中的位置(需考慮頭、身、腳佈局數量)
        mDatas.remove(position);
        if (lastVisible + 1 == mAdapter.getItemCount()) {
            addmore();
        }
        mAdapter.notifyItemRemoved(position);

        // 消耗事件
        return true;
    }
});