View.OnClickListener不工作
我有我的Recycler View
一个View.OnClickListener
,但每当我点击一个视图不会触发onClick
事件View.OnClickListener不工作
这里是代码:
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模型类
<?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>
顶部布局(<LinearLayout>
,为view
在代码中访问)内的<FrameLayout>
视图自己处理单击事件。
您需要从<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>
。
添加布局,我在查看项目 –
上使用适配器和回收站视图感谢您的帮助,它的工作! :) –
不客气:) –
view.setClickable(true);
view.setOnClickListener(this);
尽管这段代码可能是解决方案,[包括解释](// meta.stackexchange.com/questions/114762/explaining-entirely- code-based-答案)确实有助于提高你的文章的质量,请记住你将来会回答读者的问题,这些人可能不知道你的代码建议 –
感谢你的建议的原因,实际上,方法名称是清楚的以便我没有提及任何东西 –
这很好,只是这个答案出现在低质量审查队列。虽然我可以理解代码的作用(确保ViewHolder构造函数中的'view'在分配'onClickListener'之前是可点击的),那些刚开始使用Android的人可能不知道代码的位置或者*重新使用'setClickable()' –
这可能有帮助。上次,我在这里得到了解决方案。 https://*.com/questions/24471109/recyclerview-onclick https://*.com/questions/35970389/defining-a-recyclerviews-onclicklistener-in-an-activity –
感谢您的努力,但仍然不工作:( –
@YuganshTyagi检查我的更新答案 –