适配器中设置接口
//自定义点击事件需要接口回调
private MyOnItemClickListener listener;
public interface MyOnItemClickListener{
void onClick(View view,int i);
}
public void setMyOnItemClickListener(MyOnItemClickListener listener) {
this.listener = listener;
}
item使用接口
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onClick(view,position);
}
});
MainActivity中使用点击事件
adapter.setMyOnItemClickListener(new XRAdapter.MyOnItemClickListener() {
@Override
public void onClick(View view, int i) {
Toast.makeText(MainActivity.this,"点击了第"+(i+1)+"条数据",Toast.LENGTH_SHORT).show();
}
});