【问题标题】:View.OnClickListener not workingView.OnClickListener 不工作
【发布时间】:2017-09-26 15:02:49
【问题描述】:

我的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 模型类的布局

    <?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>
    

【问题讨论】:

标签: android android-recyclerview onclicklistener android-viewholder


【解决方案1】:

顶部布局内的&lt;FrameLayout&gt; 视图(&lt;LinearLayout&gt;,在您的代码中以view 访问)自行处理点击事件。

您需要像这样从&lt;FrameLayout&gt; 中删除android:clickable

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="150dp"
  android:layout_margin="4dp"
  android:focusable="true"
  android:foreground="?attr/selectableItemBackground">

如果没有,它会消耗点击并且不会到达父&lt; LinearLayout&gt;

【讨论】:

  • 添加了布局,我在 View Item 上使用了适配器和回收器视图
【解决方案2】:
view.setClickable(true);
view.setOnClickListener(this);

【讨论】:

  • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因
  • 感谢您的建议。其实方法名很清楚,我就不提了。
  • 没关系,只是这个答案出现在低质量审核队列中。虽然我可能理解代码的作用(在分配 onClickListener 之前确保 ViewHolder 构造函数中的 view 是可点击的),但刚开始使用 Android 的人可能不知道该代码的去向或为什么 你正在使用setClickable()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 2021-05-05
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
相关资源
最近更新 更多