Send click event from RecyclerView to parent ListFragment or ListAdapter

倾然丶 夕夏残阳落幕 提交于 2020-01-06 05:43:18

问题


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.

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.

I found this accepted answer to do that: Parent click event not firing when recyclerview clicked

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

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.

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>

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.


回答1:


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.

On the pinch to zoom thing. I have had great experience with Zoomy



来源:https://stackoverflow.com/questions/47208271/send-click-event-from-recyclerview-to-parent-listfragment-or-listadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!