Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
if (type=="dialer") { String timestamp = list.get(position).get(Constants.DATE); holder.txtTimestamp.setVisibility(View.VISIBLE); holder.imgDelete.s...
#1: Initial revision
Why some items aren't clickable in RecyclerView?
```java if (type=="dialer") { String timestamp = list.get(position).get(Constants.DATE); holder.txtTimestamp.setVisibility(View.VISIBLE); holder.imgDelete.setVisibility(View.GONE); holder.txtTimestamp.setText(getDate(Long.parseLong(timestamp),"dd/MM/yyyy hh:mm:ss")); if (Integer.parseInt(callType) == CallLog.Calls.BLOCKED_TYPE) holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.block)); if (Integer.parseInt(callType) == CallLog.Calls.BLOCKED_TYPE) holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.rejected)); if (Integer.parseInt(callType) == CallLog.Calls.OUTGOING_TYPE) holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.outgoing_call)); if (Integer.parseInt(callType) == CallLog.Calls.INCOMING_TYPE) holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.incoming_call)); if (Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE) holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.missed_call)); if (Integer.parseInt(callType) == CallLog.Calls.ANSWERED_EXTERNALLY_TYPE) holder.imgCallType.setImageDrawable(context.getResources().getDrawable(R.drawable.call_received)); }else if (type=="contact") { holder.txtTimestamp.setVisibility(View.GONE); holder.imgCallType.setVisibility(View.GONE); holder.imgDelete.setVisibility(View.GONE); }else if (type=="favourite"){ holder.txtTimestamp.setVisibility(View.GONE); holder.imgCallType.setVisibility(View.GONE); } // listener Bitmap finalBitmap = imgBitmap; holder.imgPic.setOnLongClickListener(view -> { CustomDialog imageDialog = new CustomDialog(activity,R.layout.image_dialog,"","","", finalBitmap); imageDialog.setCancelable(true); imageDialog.show(); return false; }); holder.itemView.setOnLongClickListener(view->{ if (type=="dialer"){ }else if (type=="contact"){ Intent intent = new Intent(context, ContactDetailsActivity.class); intent.putExtra("id",list.get(position).get(Constants.ID)); context.startActivity(intent); }else if (type=="favourite"){ Intent intent = new Intent(context, FavouriteContactDetailsActivity.class); intent.putExtra(Constants.FAVOURITE_ID,list.get(position).get(Constants.FAVOURITE_ID)); intent.putExtra(Constants.ID,list.get(position).get(Constants.ID)); ActivityOptions anim = ActivityOptions.makeSceneTransitionAnimation(activity); context.startActivity(intent, anim.toBundle()); } return false; }); holder.imgDelete.setOnClickListener(v->{ SqliteFavourite sqliteFavourite = new SqliteFavourite(context.getApplicationContext()); boolean check = sqliteFavourite.deleteData("",list.get(position).get(Constants.ID)); if (check) Snackbar.make(v,"Successfully deleted",Snackbar.LENGTH_SHORT).show(); else Snackbar.make(v,"Error occurred when deleting",Snackbar.LENGTH_SHORT).show(); }); holder.itemView.setOnClickListener(view->{ call(activity,phoneNumber); }); ``` I was using these code in an adapter. ```java @Override public int getItemCount() { return list.size(); } @Override public int getItemViewType(int position) { return position; } public class MyViewHolder extends RecyclerView.ViewHolder { TextView txtContactName,txtContactNumber,txtTimestamp; ImageView imgPic,imgCallType,imgDelete; ConstraintLayout contactItem; public MyViewHolder(@NonNull View itemView) { super(itemView); txtContactName = itemView.findViewById(R.id.txtName); txtContactNumber = itemView.findViewById(R.id.txtContactNumber); txtTimestamp = itemView.findViewById(R.id.txtTimestamp); imgPic = itemView.findViewById(R.id.imgContact); imgCallType = itemView.findViewById(R.id.imgCallType); contactItem = itemView.findViewById(R.id.contactItem); imgDelete = itemView.findViewById(R.id.imgDeleteContact); } } ``` Whenever I am clicking on first or second item then I can hear the clickListener. But when I click on 3rd 4th or higher item then I can't hear the clickListener. Even I had set a selector there but the selector is only working for 1st and 2nd ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/contactItem" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector" android:clickable="true"> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/imgContact" android:layout_width="50dp" android:layout_height="0dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/user_profile" app:civ_border_color="#FF000000" app:civ_border_width="2dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="SpeakableTextPresentCheck" /> <TextView android:id="@+id/txtName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="Name" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/txtContactNumber" app:layout_constraintEnd_toStartOf="@+id/imgCallType" app:layout_constraintStart_toEndOf="@+id/imgContact" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/txtContactNumber" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="Contact Number" app:layout_constraintBottom_toTopOf="@id/txtTimestamp" app:layout_constraintEnd_toStartOf="@+id/imgCallType" app:layout_constraintStart_toEndOf="@+id/imgContact" app:layout_constraintTop_toBottomOf="@+id/txtName" /> <TextView android:id="@+id/txtTimestamp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="TextView" android:textSize="12sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="@+id/txtContactNumber" app:layout_constraintStart_toEndOf="@+id/imgContact" app:layout_constraintTop_toBottomOf="@id/txtContactNumber" /> <ImageView android:id="@+id/imgCallType" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/call_received" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/imgDeleteContact" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/selector" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@android:drawable/ic_menu_delete" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> </LinearLayout> ``` When I scroll down then I can hear the listener but whenever I am on top at that layout then I can't access the listener in 3rd or more. Why it's happening? I am not getting any error in logcat. Although I can't click on some items why? I had tried to use `holder.contactItem.setOn....` but it wasn't working also. <hr/> When I scroll down I can listen the click. But whenever I am at top I can't listen. But I wonder I can click on Image. I meant `holder.imgPic.setOnLongClickListener.....`. <hr/> I have set `onTouchListener` to `itemView` but it's working. It's only not working for onCLickListener and onLongClickListener (As I said earlier it's working when I scroll down).