[英]RecyclerView not responding to a click despite touch feedback
我在recyclerview的一行中有两张卡片,这是布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:clickable="true"
android:layout_marginTop="5dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_height="wrap_content"
android:clickable="true"
app:cardMaxElevation="10dp"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
app:cardBackgroundColor="@color/md_white_1000">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:maxLines="4"
android:textColor="@color/md_teal_900"
android:textSize="15dp"
android:textStyle="bold"
android:layout_weight="1"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/thumbnail"
android:layout_marginTop="15dp"
android:src="@mipmap/ic_default_thumbnail"
android:adjustViewBounds="true"
android:layout_weight="10"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:id="@+id/download_card"
card_view:cardElevation = "10dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:clickable="true"
android:layout_marginTop="3dp"
android:layout_marginBottom="5dp"
android:foreground="?android:attr/selectableItemBackground"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
app:cardBackgroundColor="@color/md_white_1000">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:src="@mipmap/ic_download"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
当我按任意两个卡时,都有触摸反馈,但没有响应。
这是我处理适配器中的点击的方法。
public XViewHolder(View itemView, RecyclerView.Adapter adapter) {
super(itemView);
ButterKnife.bind(this, itemView);
this.adapter = adapter;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = this.getAdapterPosition();
if(v.getId() == R.id.card) {
mainView.startVideoFragment(JsonList.get(position).getURL());
}else{
DownloadStream downloadStream = new DownloadStream(
JsonList.get(position).getURL(), JsonList.get(position).getTitle());
}
}
甚至没有调用onClick方法。
将代码更改为..
public XViewHolder(View itemView, RecyclerView.Adapter adapter) {
super(itemView);
ButterKnife.bind(this, itemView);
this.adapter = adapter;
itemView.findViewById(R.id.card).setOnClickListener(this);
itemView.findViewById(R.id.download_card).setOnClickListener(this);
}
并删除该行。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
**android:clickable="true"** // remove it
android:layout_marginTop="5dp"
android:orientation="vertical">
甚至没有调用onClick方法。
这不是真的。 调用了该方法,但是您没有为R.id.card
注册ClickListener
,因此第一个防护始终为false。 在您的else
分支中,您将实例化DownloadStream
但并未对其进行任何操作。 如果要显式处理其他视图的单击事件,则必须显式注册特定子项的OnClickListener
。 例如
itemView.findViewById(R.id.card).setOnClickListener(this);
单击R.id.card
时调用onClick
,并且
itemView.findViewById(R.id.download_card).setOnClickListener(this);
如果您要处理R.id.download_card
的点击
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.