How can I solve a problem with click on item in horizontal RecyclerView?

Tags: , , ,

I have a horizontal recycler view. There are some buttons. I will provide my code below. So the problem is that when I click on a button, does not happen anything. It just thinks that I did not click to it. I have another recycler view, but vertical and the code is pretty similar, but the second one works. I am traying to find a mistake for ages. I really hope you can help me. Thank you.

in my main:

  adapter1 = new RecyclerViewAdapterContinentsList(this, idList,continentList);
            adapter1.setClickListener(new RecyclerViewAdapterContinentsList.ItemClickListener() {
                public void onItemClick(View view, int position) {

recyclerView class:

public class RecyclerViewAdapterContinentsList extends RecyclerView.Adapter<RecyclerViewAdapterContinentsList.ViewHolder>{
    private List<String> mId;
    private List<String> mName;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;
    Context context;

    RecyclerViewAdapterContinentsList(Context context, List<String> id, List<String> name) {
        this.mInflater = LayoutInflater.from(context);
        this.mId = id;
        this.mName = name;
        this.context = context;

    // inflates the row layout from xml when needed
    public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.activity_recycler_view_adapter_countinent_list, parent, false);
        return new ViewHolder(view);

    // binds the data to the TextView in each row
    public void onBindViewHolder(ViewHolder holder,final int position) {
        String id = mId.get(position);
        String name = mName.get(position);

    // total number of rows
    public int getItemCount() {
        return mName.size();

    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        Button continentBtn;

        ViewHolder(View itemView) {
            continentBtn = itemView.findViewById(;

        public void onClick(View view) {
            if (mClickListener != null) {
                mClickListener.onItemClick(view, getAdapterPosition());}

    // convenience method for getting data at click position
    String getItem(int id) {
        return mId.get(id);

    void setClickListener(RecyclerViewAdapterContinentsList.ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;

    public interface ItemClickListener {
        void onItemClick(View view, int position);


Change your ViewHolder code to attach the OnClickListener to the button and not the itemView.

ViewHolder(View itemView) 
    continentBtn = itemView.findViewById(;
    continentBtn.setOnClickListener(this);  // <-- button not itemView

In your ViewHolder, itemView is the root viewGroup of your layout R.layout.activity_recycler_view_adapter_countinent_list, a RecyclerView can listen to click events on the entire row using setOnItemClickListener.

Source: stackoverflow