OnItemClickListener
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
适配器里面添加
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mTextView.setText(mDataSet.get(position));
//判断是否设置了监听器
if(mOnItemClickListener != null){
//为ItemView设置监听器
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView,position);
}
});
}
}
private OnItemClickListener mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
this.mOnItemClickListener = mOnItemClickListener;
}
适配器调用
mAdapter = new MyAdapter(mData);
mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "click " + mData.get(position), Toast.LENGTH_SHORT).show();
}
});