View.OnClickListener 不工作
View.OnClickListener not working
我的 Recycler View
中有一个 View.OnClickListener
,但每当我单击视图时,它都不会触发 onClick
事件
这是代码:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView name;
ImageView image;
Context context;
ArrayList<Categories_Model_Class> arrayList;
public ViewHolder(View view, Context context, ArrayList<Categories_Model_Class> arrayList) {
super(view);
name = view.findViewById(R.id.category_name);
image = view.findViewById(R.id.category_image);
this.context = context;
this.arrayList = arrayList;
view.setOnClickListener(this);
Log.i("Created", "ViewHolder");
}
@Override
public void onClick(View view) {
//Pair<View, String> pair1 = Pair.create((View) this.image, this.image.getTransitionName());
Intent intent = new Intent(context, FullWallpaperViewActivity.class);
//ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity)context, pair1);
context.startActivity(intent);
Log.i("TOUCH", "TOUCH");
}
}
有什么建议吗?
CardView 模型的布局 Class
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="4dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground">
<ImageView
android:id="@+id/category_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:background="@color/colorBackground" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient" />
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-smallcaps"
android:text="Category!"
android:textColor="@android:color/white"
android:textSize="24dp" />
</FrameLayout>
</LinearLayout>
view.setClickable(true);
view.setOnClickListener(this);
顶部布局中的 <FrameLayout>
视图(<LinearLayout>
,在您的代码中作为 view
访问)正在自行处理点击事件。
您需要像这样从 <FrameLayout>
中删除 android:clickable
:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="4dp"
android:focusable="true"
android:foreground="?attr/selectableItemBackground">
如果没有,它将消耗点击并且不会到达父级< LinearLayout>
。
我的 Recycler View
中有一个 View.OnClickListener
,但每当我单击视图时,它都不会触发 onClick
事件
这是代码:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView name;
ImageView image;
Context context;
ArrayList<Categories_Model_Class> arrayList;
public ViewHolder(View view, Context context, ArrayList<Categories_Model_Class> arrayList) {
super(view);
name = view.findViewById(R.id.category_name);
image = view.findViewById(R.id.category_image);
this.context = context;
this.arrayList = arrayList;
view.setOnClickListener(this);
Log.i("Created", "ViewHolder");
}
@Override
public void onClick(View view) {
//Pair<View, String> pair1 = Pair.create((View) this.image, this.image.getTransitionName());
Intent intent = new Intent(context, FullWallpaperViewActivity.class);
//ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity)context, pair1);
context.startActivity(intent);
Log.i("TOUCH", "TOUCH");
}
}
有什么建议吗?
CardView 模型的布局 Class
<?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" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:focusable="true" android:foreground="?attr/selectableItemBackground" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="150dp" android:layout_margin="4dp" android:clickable="true" android:focusable="true" android:foreground="?attr/selectableItemBackground"> <ImageView android:id="@+id/category_image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:background="@color/colorBackground" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_gradient" /> <TextView android:id="@+id/category_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:fontFamily="sans-serif-smallcaps" android:text="Category!" android:textColor="@android:color/white" android:textSize="24dp" /> </FrameLayout> </LinearLayout>
view.setClickable(true);
view.setOnClickListener(this);
顶部布局中的 <FrameLayout>
视图(<LinearLayout>
,在您的代码中作为 view
访问)正在自行处理点击事件。
您需要像这样从 <FrameLayout>
中删除 android:clickable
:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="4dp"
android:focusable="true"
android:foreground="?attr/selectableItemBackground">
如果没有,它将消耗点击并且不会到达父级< LinearLayout>
。