【问题标题】:How to differentiate click event of the recyclerview itself with it's viewholders如何区分recyclerview本身的点击事件和它的viewholders
【发布时间】:2019-04-24 13:47:56
【问题描述】:

目前我有一个回收器视图,其中一个部分内具有匹配父宽度和网格布局。因此,我为父布局设置了单击侦听器,并为列表项设置了单击时会给出不同反应的列表项。问题是列表并不总是满的,当它未满时,我无法点击回收者视图的空白区域,因为它被回收者视图阻止了。

我已经尝试将点击监听器设置为回收器视图本身,但没有运气。我也无法将布局参数更改为包装内容,因为它必须匹配父级才能与网格布局一起放置。

实际上我对此有一个肮脏的解决方案,在 getItemCount 上,我会将计数硬编码为最大网格列大小,并创建一个空的布局视图类型。在 onBindViewholder 中,我可以验证当前列位置是否大于实际列表大小,我只需布置可以设置点击侦听器的空布局视图。我只是在想也许有一个更清洁的解决方案。

【问题讨论】:

  • 可以设置itemView.setOnClickListner来区分点击
  • 是的先生,实际上我已经想到了,但是 getItemCount() 本身应该返回列表中项目的实际计数。要在这种情况下在 itemView 上实现单击侦听器,我必须重写 getItemCount() 返回最大列的数量,并创建空布局作为第二种类型的视图。我是否正确理解了您的建议?
  • 如果可能请分享 XML 或查看层次图;是 RecyclerView 里面的 GridLayout 还是 RecyclerView 包裹在 RecyclerView 里面。
  • 抱歉,已编辑问题
  • 所以你也想为其子布局启用点击事件?我做过这种概念。

标签: android android-recyclerview


【解决方案1】:

onClickListener 设置在 Layout 里面,而不是 RecyclerView 本身。

在您的自定义布局资源文件中添加布局,给它一个 id 并在您的适配器类中设置 onClickListener。

【讨论】:

  • 是的,计划是这样的,对不起,因为我的问题不是很清楚,所以实际上我不需要点击回收站视图上的监听器。我想要的是点击监听器下的布局不会被回收器视图阻止。但是由于我必须使回收器视图的宽度匹配父级,因此即使网格列表项子项未满,它也会以某种方式阻止单击操作。
【解决方案2】:

由于 RecyclerView 扩展了 Scroll-layout,它会覆盖外层的点击设置。

我会说使用约束布局,然后在 recyclerview 下方添加一个空的 relativelayout 覆盖您的网格,它可以有这个点击监听器;

 <android.support.v7.widget.CardView
    android:id="@+id/card_view_outer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/global_dimen_16_dp"
    android:layout_marginLeft="@dimen/global_dimen_16_dp"
    android:layout_marginTop="@dimen/global_dimen_24_dp"
    android:layout_marginRight="@dimen/global_dimen_16_dp"
    app:cardBackgroundColor="@color/black12"
    app:cardCornerRadius="@dimen/global_dimen_4_dp"
    app:cardElevation="3dp">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view_inner"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:gravity="center"
        app:cardBackgroundColor="@color/white100"
        app:cardCornerRadius="@dimen/global_dimen_4_dp"
        app:cardElevation="0dp">

       <android.support.constraint.ConstraintLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
         <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent"/>
            <RelativeLayout
                android:id="@+id/layover"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@id/recyclerView" />
          </android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>

【讨论】:

  • 刚看到你的问题更新;上面的 XML 用于垂直布局.. 但在类似的行上,您可以将其用于水平布局以不同方式处理您的空白空间。
  • 感谢您的建议,对不起,它不起作用。也许是因为我必须设置我的回收器的宽度以匹配父级,或者 0dp 与父级的宽度限制。
  • @user3576118 RecyclerView 适配器的 ViewHolder 将有点击监听器到你的函数 a();而额外的空层,您将注册另一个 clicklistener 到函数 b();在你的问题中你也有水平滚动;在这种情况下,每一行都可以有多张卡片,你怎么能让它匹配宽度? UI 是水平的还是垂直的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多