借鉴资料:
http://blog.csdn.net/jwzhangjie/article/details/36868515
http://stackoverflow.com/questions/24471109/recyclerview-onclick/26196831#26196831
在Adapter中:
public interface OnItemClickLitener
{
void onItemClick(View view, int position);
}
private OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
{
this.mOnItemClickLitener = mOnItemClickLitener;
}
@Override
public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
// TODO Auto-generated method stub
<span style="white-space:pre"> </span>final ContactsModel rowData = contactModels.get(position);
viewHolder.contactName.setText(rowData.getName());
viewHolder.itemView.setTag(rowData);
<span style="white-space:pre"> </span>if (mOnItemClickLitener != null)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span> viewHolder.itemView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mOnItemClickLitener.onItemClick(viewHolder.itemView, position);
}
});
<span style="white-space:pre"> </span>}
}
Activity中:
myAdapter.setOnItemClickLitener(new OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
}
});