Binil Surendran Binil Surendran - 4 months ago 22
Android Question

Android: RecyclerView Click Listener is not getting in Adapter

I have an array of 600 items. I have put all the array items in a RecyclerView and they are scrolling well. But, when I try to access onClick listener it is not getting called. I also tried to Implement View.OnClickListener and Override onClick in my ViewHolder class, but its not working.

I want to Toast data of the clicked item in the list when user clicks one of the items in the view

Adapter code is

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private String mItem;
private TextView mTextView;

public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mTextView = (TextView) view.findViewById(R.id.item_title);
}

public void setItem(String item) {
mItem = item;
mTextView.setText(item);
}

@Override
public void onClick(View view) {
Log.d("TAG", "onClick " + getPosition() + " " + mItem);
}
}

private String[] mDataset;

public MyAdapter(String[] dataset) {
mDataset = dataset;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setItem(mDataset[position]);
}

@Override
public int getItemCount() {
return mDataset.length;
}
}

Answer
Try this..

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>  {

  public static class ViewHolder extends RecyclerView.ViewHolder
           {
    private String mItem;
    private TextView mTextView;

    public ViewHolder(View view) {
      super(view);
      mTextView = (TextView) view.findViewById(R.id.item_title);


    }

    public void setItem(String item) {
      mItem = item;
      mTextView.setText(item);
    }

    private String[] mDataset;
Context context;
    public MyAdapter(Context context,String[] dataset) {
    this.context=context;
      mDataset = dataset;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.item, parent, false);
      ViewHolder vh = new ViewHolder(v);
      return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

    holder.setOnClickListener(new ItemClicklistener(position);
      holder.setItem(mDataset[position]);
    }

    @Override
    public int getItemCount() {
      return mDataset.length;
    }


      class ItemClicklistener implements View.OnClickListener {
        private  int position;

        ItemClicklistener(int position) {

            this.position = position;
        }

        @Override
        public void onClick(View view) {
           Toast.makeText(context,"OnClick :"+position,Toast.LENGTH_SHORT).show();


        }
    }


  }