【问题标题】:RecyclerView with nested RecyclerView - make nested RecyclerView clickable as a whole view带有嵌套 RecyclerView 的 RecyclerView - 使嵌套的 RecyclerView 作为一个整体视图可点击
【发布时间】:2015-08-26 01:58:10
【问题描述】:

我使用显示条目列表的 RecyclerView。每个条目都承载另一个 RecyclerView,它是一个图像列表。

我现在想让这个嵌套的 RecyclerView 可点击,不是它的项目,而是整个视图。

我怎样才能做到这一点?

问题:

  • 为嵌套 RecyclerView 的主视图设置 onClickListener 确实有效,但前提是我在 RecyclerView 本身之外单击
  • 单击嵌套的 RecyclerView 不会将单击传递给父视图(我什至尝试将 clickable、focusable、focusableIntouch 设置为 false,但触摸不是委托而是由嵌套的 RecyclerView 消耗...李>

这是包装适配器的视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="0dp"
    android:layout_margin="5dp"
    android:foreground="?android:attr/selectableItemBackground" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/rlTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Datum"
                android:textStyle="bold"
                android:id="@+id/tvDate" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Info"
                android:id="@+id/tvInfo" />
        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvData"
            android:clickable="false"
            android:focusableInTouchMode="false"
            android:focusable="false"
            android:layout_below="@+id/rlTop"
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:scrollbars="horizontal" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

【问题讨论】:

  • 你想让内部回收站视图是水平的吗?
  • 是的。它显示了一排图片...
  • 我没有完全理解。你想要某种 onItemClickListener 吗?整行?
  • 正是...我希望整行都可以点击。将onClickListener 设置为行有效,但嵌套RecyclerView 消耗的区域没有反应,只有视图的其余部分...

标签: android onclicklistener android-recyclerview


【解决方案1】:

我查看了 RecyclerView 源代码,这有所帮助:

recyclerView.setLayoutFrozen(true)

【讨论】:

  • 但是,这会阻塞recyclerview的滚动事件。
【解决方案2】:

这是一个老问题,但我有同样的任务,但没有找到好的解决方案。 我只是将透明视图放在 recyclerview 上并将点击侦听器设置为此视图。

<android.support.v7.widget.CardView
    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:orientation="vertical"
    app:cardBackgroundColor="@color/white"
    app:cardCornerRadius="0dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/dataLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/history_margin_v"
            android:paddingEnd="@dimen/history_margin_h"
            android:paddingLeft="@dimen/history_margin_h"
            android:paddingRight="@dimen/history_margin_h"
            android:paddingStart="@dimen/history_margin_h"
            android:paddingTop="@dimen/history_margin_v">

            <TextView
                android:id="@+id/day"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/widget_margin_v"
                android:textColor="@color/font_blue"
                android:textSize="@dimen/font_size_large"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/images"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <View
            android:id="@+id/clickView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:layout_alignLeft="@+id/dataLayout"
            android:layout_alignRight="@+id/dataLayout"
            android:layout_alignStart="@+id/dataLayout"
            android:layout_alignEnd="@+id/dataLayout"
            android:layout_alignTop="@+id/dataLayout"
            android:layout_alignBottom="@+id/dataLayout"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>

【讨论】:

  • 但是,这会阻止recyclerview项目的滚动事件。
【解决方案3】:

为了使整行可点击,而不是每张图片,您需要实现自定义 onItemClickListener(名称来自 listview,抱歉)。 看看这个link 这对我来说很完美。

编辑:

嵌套回收器会窃取您的点击次数。为了解决这个问题,您需要创建一个自定义触摸侦听器并将其传递给嵌套回收器。就像你放在外部 ercycler 中但将事件传递给外部的那个。

编辑 2:

添加一个具有两种状态的选择器,就像一个按钮到整个行,然后在内部回收器上的 onclick setSelected(true) 行和 postDelay 50ms for setSelected(false ) 这将产生“点击”效果。

【讨论】:

  • 这使得该行可点击,但只有没有被嵌套RecyclerView占用的区域...
  • 致您的编辑:这是我目前正在使用的解决方法...缺点;点击嵌套的RecyclerView 不提供视觉反馈...如果我也点击嵌套的RecyclerView,该行应该提供点击反馈...
猜你喜欢
  • 1970-01-01
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 2020-03-18
  • 1970-01-01
相关资源
最近更新 更多