我的Clicklistener
无效。我想将ClickListener
添加到RecyclerView
。
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView nameView;
public ViewHolder(View itemView) {
super(itemView);
nameView = (TextView) itemView.findViewById(R.id.nameText);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
ListItems items = listItems.get(position);
Toast.makeText(context, items.getName(), Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
public class ViewHolder extends RecyclerView.ViewHolder
{
public TextView nameView;
public ViewHolder(View itemView) {
super(itemView);
nameView = (TextView) itemView.findViewById(R.id.nameText);
itemView.setOnClickListener(this);
}
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do evnt listening here
Toast.makeText(v.getContext(), "position = " + getItemId(position), Toast.LENGTH_SHORT).show();
}
});
}
}