这不是我第一次为RecyclerView实现适配器,所以我不理解这个问题。
我让回收器视图使用自定义列表项填充项目,我还设置了Viewholder itemView.setOnClickListener,当我单击项目时,它不会注册点击。
但是,当我在项目上的文本上方单击时,它会注册单击...也许与我的自定义项目布局有关?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/spacing_small"
android:layout_marginRight="@dimen/spacing_small"
android:layout_marginTop="@dimen/spacing_small">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
android:clickable="true"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Plan ID:" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username:" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Timestamp:" />
</LinearLayout>
<View
android:layout_width="@dimen/spacing_medium"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/plan_id_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/username_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/timestamp_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageButton
android:id="@+id/options"
android:layout_width="wrap_content"
android:layout_height="@dimen/spacing_xlarge"
android:background="?attr/selectableItemBackgroundBorderless"
android:tint="@color/grey_40"
app:srcCompat="@drawable/ic_more_vert" />
<View
android:layout_width="@dimen/spacing_small"
android:layout_height="0dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这是我的适配器:
@Override
public void onClick(View v) {
final TextView timestamp_tv = v.findViewById(R.id.timestamp_tv);
switch (form_name) {
case "daily_worksheet":
Bundle bundle = new Bundle();
bundle.putString("timestamp", timestamp_tv.getText().toString());
new LoadActivityTask((Activity) context, new Intent(context, DailyWorksheet_View.class), "Daily Worksheet Form", bundle).execute();
break;
default:break;
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView plan_id, username, timestamp;
private ImageView options;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(ReceiptAdapter.this);
plan_id = (TextView) itemView.findViewById(R.id.plan_id_tv);
username = (TextView) itemView.findViewById(R.id.username_tv);
timestamp = (TextView) itemView.findViewById(R.id.timestamp_tv);
options = (ImageView) itemView.findViewById(R.id.options);
}
}
发布于 2018-08-03 02:42:11
我认为错误是因为您在布局的根上设置了OnClickListener,而您的CardView基本上是布局的主背景,无法单击,因此影响OnClicklistener的唯一空间是CardView的小边距。
我认为您应该在Cardview上设置OnClicklistener (并设置为可点击)。
itemView.findViewById(R.id.cardView).setOnClickListener(ReceiptAdapter.this);
发布于 2018-08-03 05:58:23
尝试删除第一个LinearLayout中的android:clickable="true“:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/spacing_small"
android:layout_marginRight="@dimen/spacing_small"
android:layout_marginTop="@dimen/spacing_small">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
android:orientation="horizontal">
...
https://stackoverflow.com/questions/51659800
复制相似问题