我在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));
}
}
我已经尝试附加onAction事件,但是我需要让OnClickListener工作。有人可以帮我解决这个问题吗?谢谢
来自RecyclerView.ViewHolder文档:
ViewHolder描述了一个项目视图和有关其在RecyclerView中的位置的元数据。 RecyclerView.Adapter实现应该是ViewHolder的子类,并添加用于缓存潜在昂贵的findViewById(int)结果的字段。
ViewHolder自定义实现主要用于缓存目的,这意味着下面的行无效,因为您将侦听器设置为数据存储容器。
itemView.setOnClickListener(this);
解决方案是将侦听器设置为实际的itemView(可能就在您调用ChatHolder(itemView)之前)。
不幸的是我无法确定请发布您的自定义回收站视图的代码。
所以我解决了这个问题,得到了官方github Repo of SwipeRevealLayout的帮助。这是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>