pro*_*m85 6 android onclicklistener android-recyclerview
我使用显示条目列表的 RecyclerView。每个条目托管另一个 RecyclerView,它是一个图像列表。
我现在想让这个嵌套的 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>
Run Code Online (Sandbox Code Playgroud)
我查看了 RecyclerView 源代码,这有帮助:
recyclerView.setLayoutFrozen(true)
Run Code Online (Sandbox Code Playgroud)
为了使整行可点击,而不是每个图像,您需要实现一个自定义的onItemClickListener(名称来自 listview ,抱歉)。看看这个链接, 这对我来说非常完美。
编辑:
嵌套回收器会窃取您的点击次数。为了解决这个问题,您需要创建一个自定义触摸监听器并将其传递给嵌套回收器。就像您放入外部 ercycler 的事件一样,但将事件传递给外部。
编辑2:
添加一个具有两种状态的选择器,就像整个行中的按钮一样,然后在内部回收器setSelected(true)上的 onclick 处添加该行,并为setSelected(false)设置 50ms 的 postDelay ,这将产生“点击”效果。
| 归档时间: |
|
| 查看次数: |
5712 次 |
| 最近记录: |