該系列文章  如果想全方面學習,建議參考這個大牛的文章,寫的真可以。

地址:http://blog.csdn.net/lmj623565791/article/details/45059587

因為RecyclerView沒有點選、長按事件,需要自己寫


實現的方式比較多,可以通過adapter中自己去提供回撥

廢話不多說,直接看程式碼:

public class GalleryAdapter extends	RecyclerView.Adapter<GalleryAdapter.ViewHolder> {

	/**
	 *定義回撥介面
	 */
	public interface OnItemClickLitener {
		void onItemClick(View view, int position);//點選事件
	}
	/**
	 * 單擊 點選監聽事件變數
	 */
	private OnItemClickLitener mOnItemClickLitener;

	/**
	 * 設定
	 * @param mOnItemClickLitener
	 */
	public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
		this.mOnItemClickLitener = mOnItemClickLitener;
	}

	private LayoutInflater mInflater;//佈局填充器
	private List<Integer> mDatas;//介面卡繫結的資料

	/**
	 * 建構函式
	 */
	public GalleryAdapter(Context context, List<Integer> datats) {
		mInflater = LayoutInflater.from(context);
		mDatas = datats;
	}

	/**
	 *ViewHolder 跟listView裡面一樣  屬性是item 佈局裡面各個檢視控制元件
	 */
	public static class ViewHolder extends RecyclerView.ViewHolder {
		public ViewHolder(View arg0) {
			//這裡面可以先找到各個檢視控制元件,
			super(arg0);
		}

		ImageView mImg;
		TextView mTxt;
	}

	@Override
	public int getItemCount() {
		return mDatas.size();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
		//找到對應的佈局item
		View view = mInflater.inflate(R.layout.activity_index_gallery_item,viewGroup, false);
		ViewHolder viewHolder = new ViewHolder(view);
		viewHolder.mImg = (ImageView) view.findViewById(R.id.id_index_gallery_item_image);
		return viewHolder;
	}

	/**
	 * 該函式用來進行資料填充的,同時如果有監聽事件,也在這裡面
	 */
	@Override
	public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
		// 佈局填充資料
		viewHolder.mImg.setImageResource(mDatas.get(i));
		// 如果設定了回撥,則設定點選事件
		if (mOnItemClickLitener != null) {
			viewHolder.itemView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//設定點選事件 具體的呼叫執行,由當前的adapter例項進行操作
					mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
				}
			});
		}

	}

}
在對應的activity中adapter例項呼叫點選事件:
        mAdapter.setOnItemClickLitener(new OnItemClickLitener()
        {

            @Override
            public void onItemClick(View view, int position)
            {
                Toast.makeText(HomeActivity.this, position + " click",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position)
            {
                Toast.makeText(HomeActivity.this, position + " long click",
                        Toast.LENGTH_SHORT).show();
                        mAdapter.removeData(position);
            }
        });


該處程式碼只展示了 點選事件,長按事件是一樣的做法,無非就是多一個方法了:
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

然後在onBindViewHolder裡面加入設定就可以:
            viewHolder.itemView.setOnLongClickListener(new OnLongClickListener()
            {
                @Override
                public boolean onLongClick(View v)
                {
                    mOnItemClickLitener.onItemLongClick(viewHolder.itemView, i);
                }
            });