RecycleView 添加item点击事件

1.RecycleView点击事件的实现

以常用的瀑布流控件pullloadmorerecyclerview 为例
1.实现 OnClickListener事件
public class StaggeredRecycleViewAdapter extends RecyclerView.Adapter implements View.OnClickListener

2.添加监听
private OnItemClickListener mOnItemClickListener = null;
public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
3.onCreateViewHolder添加监听
 
   
v.setOnClickListener(this);
 4. 
   onBindViewHolder添加标签位置 
   
holder.itemView.setTag(position);

2.完整代码

/**
 * Created by dingkangkang on 2017/6/7.
 */

public class StaggeredRecycleViewAdapter extends RecyclerView.Adapter implements View.OnClickListener{

    private Context mContext;
    private List> dataList;

    private OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }
    public List> getDataList() {
        return dataList;
    }

    public StaggeredRecycleViewAdapter(Context context, List> dataList) {
        this.dataList = dataList;
        mContext = context;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView title;
        public SketchImageView iv_tupian;


        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            iv_tupian = (SketchImageView) itemView.findViewById(R.id.iv_tupian);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.staggered_recycler_view_item, parent, false);
        v.setOnClickListener(this);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.iv_tupian.displayImage(dataList.get(position).get("imageurl"));
//        holder.title.setText(dataList.get(position).get("text"));
//        holder.title.setHeight(Integer.parseInt(dataList.get(position).get("height")));//高度随机,下拉刷新高度会变
        //将position保存在itemView的Tag中,以便点击时进行获取
        holder.itemView.setTag(position);
    }

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

}

3.activity中调用

mRecyclerViewAdapter.setOnItemClickListener(new StaggeredRecycleViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getActivity(),"第几条:::"+position,Toast.LENGTH_SHORT).show();
            }
        });


你可能感兴趣的:(Android开发)