简体   繁体   中英

OnClickListener not getting invoked in recycler view

I am using a SwipeRevealLayout for a single item in a recyclerview and also want to attach an OnClickListener on it. This is my xml file of layout of a single item:

<com.chauthai.swipereveallayout.SwipeRevealLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:mode="normal"
    app:dragEdge="left"
    android:clickable="true">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:padding="8dp"
        android:gravity="center_vertical"
        android:clickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_archive"
            android:clickable="false"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Archive"
            android:id="@+id/id_text_archive"
            android:textSize="24sp"
            android:textColor="@color/colorTextLight"
            android:clickable="false"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:padding="8dp"
        android:clickable="false">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test Chat Name"
            android:textSize="24sp"
            android:textColor="@android:color/black"
            android:id="@+id/id_text_chat_head"
            android:clickable="false"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test Chat Name"
            android:textSize="18sp"
            android:textColor="@color/colorChatroomSubTitle"
            android:id="@+id/id_text_chat_sub_head"
            android:clickable="false"/>

    </LinearLayout>

</com.chauthai.swipereveallayout.SwipeRevealLayout>

Here is my viewholder code:

public class ChatHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        @BindView(R.id.id_text_chat_head)
        TextView mChatHead;
        @BindView(R.id.id_text_chat_sub_head)
        TextView mChatSubHead;
        @BindView(R.id.id_text_archive)
        TextView mTextArchive;

        private View mView;

        private AnonymousReport mAnonymousReport;

        public ChatHolder(@NonNull View itemView) {
            super(itemView);

            mView = itemView;

            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        public SwipeRevealLayout getSwipeRevealLayout() {
            return ((SwipeRevealLayout) mView);
        }

        public void bindView(AnonymousReport report) {
            mAnonymousReport = report;

            mChatHead.setText(report.getSubject());
            mChatSubHead.setText(report.getAnonTypeText(mContext));

            mTextArchive.setText(isArchived ? "Unarchive" : "Archive");
        }

        @Override
        public void onClick(View v) {
            startActivity(AnonymousReportDetailsActivity.newIntent(mContext, mAnonymousReport));
        }
    }

I have tried attaching onActionEvent and that works but I need t get OnClickListner to work. Can someone please help me figure out the issue? Thanks

From RecyclerView.ViewHolder documentation:

A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView.Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById(int) results.

ViewHolder custom implementations are mostly used for caching purposes which means that the line below has no effect because you are setting the listener to a data store container.

itemView.setOnClickListener(this);

The solution is to set the listener to the actual itemView (probably just before you call ChatHolder(itemView)).

Unfortunately I cannot be sure therefore please post the code for your custom recycler view.

So I solved the issue, got help from the official github Repo of SwipeRevealLayout. Here's link to original post Here's what it said:

You can set onClick for main layout and secondary layout, but you can't set onClick for the whole swipeRevealLayout.

So I modified my code accordingly and it worked.

<com.chauthai.swipereveallayout.SwipeRevealLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:mode="normal"
    app:dragEdge="left">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:padding="8dp"
        android:gravity="center_vertical"
        android:id="@+id/id_chat_room_item"
        android:clickable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_archive"
            android:clickable="false"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Archive"
            android:id="@+id/id_text_archive"
            android:textSize="24sp"
            android:textColor="@color/colorTextLight"
            android:clickable="false"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:padding="8dp"
        android:clickable="false">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test Chat Name"
            android:textSize="24sp"
            android:textColor="@android:color/black"
            android:id="@+id/id_text_chat_head"
            android:clickable="false"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test Chat Name"
            android:textSize="18sp"
            android:textColor="@color/colorChatroomSubTitle"
            android:id="@+id/id_text_chat_sub_head"
            android:clickable="false"/>

    </LinearLayout>

</com.chauthai.swipereveallayout.SwipeRevealLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM