public class MyAdapter extends RecyclerView.Adapterimplements View.OnClickListener { private ArrayListmList; public MyAdapter(ArrayList mList) {
this.mList = mList;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_item, null);
Holder holder = new Holder(view);
//给这个VIew注册点击事件了
view.setOnClickListener(this);
return holder;
}
@Override
public void onBindViewHolder(Holder holder, int position) {
holder.mText.setText(mList.get(position));
//itemVIew 跟 View一个东西 传入标记
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return mList.size();
}
public class Holder extends RecyclerView.ViewHolder {
private TextView mText;
public Holder(View itemView) {
super(itemView);
mText = itemView.findViewById(R.id.Recy_Name);
}
}
public interface OnItemClick {
void setOnItemClick(View v, int position);
}
private OnItemClick mOnItem;
@Override
public void onClick(View v) {
if (mOnItem != null) {
mOnItem.setOnItemClick(v, (int) v.getTag());
}
}
public void setOnItem(OnItemClick onItem) {
this.mOnItem = onItem;
}
}
网友评论