RecyclerView中的item点击事件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Dimples_54/article/details/69396442

1.在adapter中:

private RecyclerView rv;

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    rv = recyclerView;
}
public interface OnItemClickListener {
    void onItemClick(int postion);
}
private OnItemClickListener mOnItemClickListener = null;

public void setOnItemClickListener(Find_Item2Adapter.OnItemClickListener listener) {
    this.mOnItemClickListener = listener;
}

2.在onCreateViewHolder中:

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.find_list_item2_item,parent,false);
view.setOnClickListener(this);
return new Myfinditem2Viewholder(view);

3.onClick方法

@Override
public void onClick(View v) {
    if (mOnItemClickListener != null){
        mOnItemClickListener.onItemClick(rv.getChildAdapterPosition(v));
    }
}

4.在activity中:

adapter.setOnItemClickListener(new Find_Item2Adapter.OnItemClickListener() {
    @Override
    public void onItemClick(int position) {
        Log.e("onItemClick: ","position:=="+position );
    }
});

展开阅读全文

没有更多推荐了,返回首页