Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Comments on Why some items aren't clickable in RecyclerView?

Post

Why some items aren't clickable in RecyclerView?

+0
−0

        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.


    @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 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.


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......


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).

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Typos? (1 comment)
Typos?
mcalex‭ wrote almost 3 years ago

Prolly just a typo, but you have BLOCKED_TYPE for both R.drawable.block and R.drawable.rejected. Also, is getItemViewType() correct? It takes an int and returns that int?

- I don't think this solves the problem, however