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
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!
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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With