我的点击事件无效

时间:2017-10-08 13:26:52

标签: android

我的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();
    }
}

1 个答案:

答案 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();
        }
    });
}    

}