RecyclerView中SwipeRevealLayout项的OnClickListener无法触发求助
解决SwipeRevealLayout列表项点击事件不触发的问题
我来帮你搞定这个点击事件失效的问题,咱们从布局和代码两个角度调整:
问题根源
SwipeRevealLayout作为滑动容器,默认会拦截触摸事件来处理滑动逻辑,导致你绑定在itemView(也就是SwipeRevealLayout本身)上的点击事件被“吃掉”了。再加上你给SwipeRevealLayout设置了android:clickable="true",反而会进一步干扰事件传递。
解决方案一:绑定内容布局的点击事件(推荐)
最稳妥的方式是把点击事件绑定到列表项的内容区域(也就是你布局里白色背景的LinearLayout),而不是SwipeRevealLayout本身:
- 修改列表项XML布局:
- 移除SwipeRevealLayout的
android:clickable="true"属性 - 给内容区域的LinearLayout添加id,并设置
android:clickable="true"
- 移除SwipeRevealLayout的
修改后的布局代码:
<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: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> <!-- 内容区域:添加id并设置clickable为true --> <LinearLayout android:id="@+id/id_content_container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:padding="8dp" android:clickable="true"> <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>
- 更新ViewHolder代码:
- 新增内容布局的绑定
- 把点击事件绑定到内容布局上,而不是itemView
- 顺便初始化
mContext(你的代码里漏了这一步,会导致startActivity报错)
修改后的ViewHolder代码:
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; @BindView(R.id.id_content_container) LinearLayout mContentContainer; // 新增绑定 private View mView; private AnonymousReport mAnonymousReport; private Context mContext; // 新增Context变量 public ChatHolder(@NonNull View itemView) { super(itemView); mView = itemView; mContext = itemView.getContext(); // 初始化Context ButterKnife.bind(this, itemView); mContentContainer.setOnClickListener(this); // 绑定内容布局的点击事件 } public SwipeRevealLayout getSwipeRevealLayout() { return ((SwipeRevealLayout) mView); } public void bindView(AnonymousReport report, boolean isArchived) { // 补充isArchived参数(原代码里用到但未传入) 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)); } }
解决方案二:调整SwipeRevealLayout的事件拦截
如果你一定要绑定SwipeRevealLayout本身的点击事件,可以通过自定义它的触摸事件处理,或者使用它提供的setOnClickListener方法,同时确保滑动手势结束后如果没有滑动距离就触发点击。不过这种方式不如第一种直接,更推荐方案一。
内容的提问来源于stack exchange,提问作者Hashir Baig
相关产品推荐
相关产品推荐

