记录一下,方便直接用
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder>{
private OnItemClickListener OnItemClickListener;
public TestAdapter( ) {
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=mLayoutInflater.inflate(R.layout.test_item,null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
}
@Override
public int getItemCount() {
return list.size();
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
OnItemClickListener = onItemClickListener;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private OnItemClickListener mOnItemClickListener;
ViewHolder(View itemView) {
super(itemView);
mOnItemClickListener=OnItemClickListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mOnItemClickListener == null) {
return;
}
mOnItemClickListener.onClick(v,getLayoutPosition());
}
}
public interface OnItemClickListener {
public void onClick(View view, int position);
}
}