Junaid
Junaid

Reputation: 4832

Row item in recycler view does not respond to click event

Following is the layout of my row item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">


    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img_query"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="left|top"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="2dp"
            android:src="@drawable/ic_anoun_red" />

        <TextView
            android:id="@+id/txt_ques_sub"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:layout_marginTop="12dp"
            android:layout_toRightOf="@+id/img_query"
            android:text="Problem in Merge sort"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/primary_text_light"
            android:textStyle="bold" />



        <TextView

            android:id="@+id/txt_ques_detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="2dp"
            android:layout_below="@+id/img_query"
            android:text="@string/sample_query_detail"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/primary_text_light" />

        <TextView
            android:id="@+id/txt_asked_by"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_ques_detail"
            android:layout_marginRight="4dp"
            android:layout_marginTop="2dp"
            android:text="Tabish Butt"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/lightorange" />

        <TextView
            android:id="@+id/txt_ques_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_asked_by"
            android:layout_marginRight="4dp"
            android:text="07:15 am"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/secondary_text_dark" />

        <TextView
            android:id="@+id/txt_ques_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_ques_time"
            android:layout_marginRight="4dp"
            android:text="25 SEP 2015"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/secondary_text_dark" />

        <TextView
            android:id="@+id/txt_replies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/txt_ques_date"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="4dp"
            android:text="@string/default_num_answers"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/primary_dark_material_light" />


        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_below="@+id/txt_replies"
            android:alpha="0.12"
            android:background="@android:color/black" />
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/divider"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_answer"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:drawableLeft="@android:drawable/ic_media_play"
                android:text="ANSWER"
                android:textColor="@color/primary_dark_material_light" />
        </LinearLayout>
    </RelativeLayout>
    </LinearLayout>

</LinearLayout>

Following is the onClick code:

holder.setClickListener(new MyViewHolder.ClickListener() {
            @Override
            public void onClick(View v, int position, boolean isLongClick) {
                if(isLongClick)
                {
                    /* TASK TO PERFORM AT LONG CLICKS */
                    Toast.makeText(context, "Long click acheived", Toast.LENGTH_LONG).show();
                }
                else
                {
                    /* TASK TO PERFORM AT CLICKS */
                    Toast.makeText(context, "Click Event", Toast.LENGTH_LONG).show();
                }
            }
        });

Now when i run this everything works fine except that clicking a row item does not generate any response (toasts in this case)

any help will be greatly appreciated. Need help !

Following is the method through which click events are handled in MyViewHolder class:

static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {

    //Following represent grid_layout item
    TextView txtFileName;
    TextView txtFileIssueTime;
    Button btnAnswer;
    private ClickListener clickListener;
    public MyViewHolder(View itemView) {
        super(itemView);

        //VIEWS IN LAYOUT BEING INITIALIZED

        btnAnswer = (Button) itemView.findViewById(R.id.btn_answer);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }

    /* Interface for handling clicks - both normal and long ones. */
    public interface ClickListener {

        /**
         * Called when the view is clicked.
         *
         * @param v view that is clicked
         * @param position of the clicked item
         * @param isLongClick true if long click, false otherwise
         */
        public void onClick(View v, int position, boolean isLongClick);

    }

    /* Setter for listener. */
    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public void onClick(View v) {

        // If not long clicked, pass last variable as false.
        clickListener.onClick(v, getAdapterPosition(), false);
    }

    @Override
    public boolean onLongClick(View v) {

        // If long clicked, passed last variable as true.
        clickListener.onClick(v, getAdapterPosition(), true);
        return true;
    }

}

Upvotes: 2

Views: 3344

Answers (2)

Jin
Jin

Reputation: 6145

Try moving the clickable to be in the parent LinearLayout, not the nested LinearLayout

Edit:

actually, your layout should just be

<FrameLayout
    android:background:"@color/white"
    android:foreground:"?selectableItemBackground">

    <RelativeLayout />

</FrameLayout>

Upvotes: 1

Daniel Ocampo
Daniel Ocampo

Reputation: 256

There's no need to set the click listeners to the itemView, you're already implementing the clicklisteners in the ViewHolder.

I am not sure still from where you're calling the "holder.setClickListener(...)", but assuming you're doing so from an Activity, I would rather follow this approach:

1) Have the ClickListener as an independent interface (outside your holder), you could use it in a different adapter later.

2) Add a ClickListener to both your adapter and your ViewHolder.

3) When setting up the Adapter, pass your Activity/Fragment as the listener (they should implement the ClickListener interface)

4) When creating the ViewHolder, pass the listener as well to the ViewHolder and set it to the variable (that you already have defined).

5) As you already do, in the onClick methods, call the clickListener methods.

Let me know if that works. It's exactly what I often do.

Upvotes: 1

Related Questions