将点击事件从RecyclerView发送到父ListFragment或ListAdapter [英] Send click event from RecyclerView to parent ListFragment or ListAdapter
问题描述
我有一个带有某些ListItems的ListFragment. 在一个ListItem中,我有一些视图以及带有CardViews的RecyclerView. 在CardView中是ImageView.
I have a ListFragment with some ListItems. In one ListItem I have some Views and also a RecyclerView with CardViews. In the CardView is a ImageView.
我想使用RecyclerView包含一些图片,并且想要滚动图片.我也想用两个手指手势来缩放图片. (如果有人知道一个很好的循序渐进教程,那将是很好的).但我也想单击并长按图片.我需要在ListArrayAdapter或ListFragment中单击并长按.
I want to use the RecyclerView to have some pictures in it and I want to scroll the pictures. I also want to zoom the pictures with a two finger-gesture. (Would be nice if someone knows a good step by step tutorial therefor). But I also want to click and longclick on the pictures. I need the click and longclick in my ListArrayAdapter or in my ListFragment.
我找到了这个公认的答案来做到这一点: 点击recyclerview时未触发父级点击事件
I found this accepted answer to do that: Parent click event not firing when recyclerview clicked
但是点击次数不会到达Adapter或ListFragment.而且,如果我使用LongClick多玩一点,则应用程序会崩溃,因为CardView调用performLongClick并以NullPointerException结尾:
But the clicks doesn't reach the Adapter or ListFragment. And if I play a little more with the LongClick the App crashes because the CardView calls performLongClick and this ends in a NullPointerException:
java.lang.NullPointerException:尝试调用接口方法'boolean android.view.ViewParent.showContextMenuForChild(android.view.View)'上为null 对象参考 在android.view.View.showContextMenu(View.java:6346) 在 android.view.View.performLongClickInternal(View.java:6280) 在android.view.View.performLongClick(View.java:6234)
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.ViewParent.showContextMenuForChild(android.view.View)' on a null object reference at android.view.View.showContextMenu(View.java:6346) at android.view.View.performLongClickInternal(View.java:6280) at android.view.View.performLongClick(View.java:6234)
那么-我需要做更多的工作以使一切正常吗?
So - what I have to do more that everything works fine?
这是行布局-缩小了一些:
This is the rowlayout - a little bit shrinked:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="ListFragment"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left|center_vertical"
android:layout_marginLeft="5dp"
android:minHeight="30dp"
android:orientation="vertical"
android:layout_toLeftOf="@+id/icon"
>
<RelativeLayout
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="wrap_content"
android:layout_height="350dp"
/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
在ListAdapter中,我的myLayout-View和myRecyclerView-View有一个OnClickListener(onClick(View)). 在ListFragment中,我的onListItemClick方法中也有一个断点.但是什么也没发生.
I have a OnClickListener (onClick(View)) for the myLayout-View and for the myRecyclerView-View in the ListAdapter. In the ListFragment I also have a breakpoint in my onListItemClick-method. But nothing happend.
CardView布局:
The CardView layout:
<com.package.MyCardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp"
android:descendantFocusability="blocksDescendants"
>
<ImageView android:id="@+id/picture"
android:src="@drawable/picture_empty"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:adjustViewBounds="true"
android:visibility="invisible"
xmlns:android="http://schemas.android.com/apk/res/android" />
</com.package.MyCardView>
您可以在上面的链接中的接受的答案中看到MyCardView的来源.调用了Initialize(),如果我在ImageView/CardView上短按,则调用了onInterceptTouchEvent(),但是它具有ACTION_DOWN事件,然后调用了onDown()和onLongPress().
The source of MyCardView you can see in the accepted answer in the link above. The Initialize() were called and if I touch short on the ImageView/CardView the onInterceptTouchEvent() were called but has an ACTION_DOWN event and then the onDown() and then the onLongPress() were called.
推荐答案
您应该发布更多代码,以便我们更好地调试此问题.
我不知道您在做什么,但是通常的做法是使适配器/片段实现View.OnClickListener
和View.OnLongClickListener
并将其附加到ViewHolders
中的适当视图上.
You should post more code to allow us to better debug this issue.
I don't know what you are doing, but the usual pattern how to do this is to make your adapter/fragment implement View.OnClickListener
and View.OnLongClickListener
and attach that in your ViewHolders
to appropriate views.
在捏放大的东西.我在 Zoomy
On the pinch to zoom thing. I have had great experience with Zoomy
这篇关于将点击事件从RecyclerView发送到父ListFragment或ListAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!