【问题标题】:OnClickListener not getting invoked in recycler viewOnClickListener 没有在回收站视图中被调用
【发布时间】:2019-03-29 23:34:11
【问题描述】:

我正在为 recyclerview 中的单个项目使用 SwipeRevealLayout,并且还想在其上附加 OnClickListener。这是我的单个项目布局的xml文件:

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

这是我的查看器代码:

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));
        }
    }

我已经尝试附加 onActionEvent 并且可以正常工作,但我不需要让 OnClickListner 工作。 有人可以帮我解决这个问题吗?谢谢

【问题讨论】:

    标签: java android android-recyclerview onclicklistener


    【解决方案1】:

    来自 RecyclerView.ViewHolder 文档:

    ViewHolder 描述了一个项目视图和关于它在 RecyclerView 中的位置的元数据。 RecyclerView.Adapter 实现应该继承 ViewHolder 并添加字段以缓存可能昂贵的 findViewById(int) 结果。

    ViewHolder 自定义实现主要用于缓存目的,这意味着下面的行无效,因为您将侦听器设置为数据存储容器。

    itemView.setOnClickListener(this);
    

    解决方案是将侦听器设置为实际的 itemView(可能就在您调用 ChatHolder(itemView) 之前)。

    很遗憾,我无法确定,因此请发布您的自定义回收站视图的代码。

    【讨论】:

      【解决方案2】:

      所以我解决了这个问题,从 SwipeRevealLayout 的官方 github Repo 获得了帮助。 这是link to original post 它是这样说的:

      你可以为主布局和次要布局设置onClick,但是你 无法为整个 swipeRevealLayout 设置 onClick。

      所以我相应地修改了我的代码并且它工作了。

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

      【讨论】:

        猜你喜欢
        • 2015-06-12
        • 1970-01-01
        • 2013-12-07
        • 1970-01-01
        • 1970-01-01
        • 2020-11-26
        • 2021-10-01
        • 2017-12-21
        • 1970-01-01
        相关资源
        最近更新 更多