Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclicklistener not working on recyclerview

I am working with recyclerview the same code given below was working fine for me but I have tried following code but it is not working

recyclerView = (RecyclerView)v.findViewById(R.id.datelist);
    recyclerView.setClickable(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);
    adapter.setOnItemClickListener(this);

in the Adapter

public  class UpdateHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener,CompoundButton.OnCheckedChangeListener {
        TextView uname;
        TextView uversn;
        TextView usitting;


        public UpdateHolder(View itemView) {
            super(itemView);
            uname = (TextView) itemView.findViewById(R.id.update_name);
            uversn = (TextView) itemView.findViewById(R.id.update_version);
            usitting=(TextView) itemView.findViewById(R.id.sitting_type);

            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            myClickListener.onItemClick(getPosition(), v);
        }
}
public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

I have also tried the in Adapter in onCreateViewHolder

Please help me out

like image 921
Tushar Avatar asked Aug 14 '15 07:08

Tushar


People also ask

How to handle click event on RecyclerView?

Update the ViewHolder to take in onClick() as a parameter. In the initializer, call setOnClickListener{} on the itemView . That's it! Your RecyclerView is now responsive so time to get your click on!

How to add click listener on button in RecyclerView Android?

In your ViewHolder class in your RecyclerView adapter, implement View. OnClickListener, bind the listener to the view. In the onClick method, call the onClick method of the interface OnItemClickListener. This should be passed in from your RecycyclerView's constructor.


2 Answers

For me, the solution was to remove the below from the CardView xml file.

android:clickable="true"

Apparently, this attribute overrides any clickListeners added to the view.

like image 126
Neutrino Avatar answered Sep 28 '22 08:09

Neutrino


In recyclerView you have to create viewHolder like below code, you have to set click listener on viewHolder View as seen in below.

public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        private final MyEditText edtTest;

        public ViewHolder(View v) {
            super(v);
            // Define click listener for the ViewHolder's View.
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "Element " + getPosition() + " clicked.");
                }
            });
            v.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Log.e("Demo", "Long click done bro--------------------->");
                    return false;
                }
            });
            textView = (TextView) v.findViewById(R.id.textView);
            edtTest = (MyEditText) v.findViewById(R.id.edtTest);
        }

        public TextView getTextView() {
            return textView;
        }

        public EditText getEdtTest() {
            return edtTest;
        }
    }
like image 34
Darshan Mistry Avatar answered Sep 28 '22 08:09

Darshan Mistry