GridView onItemClickListener and onItemSelected do not perform callback functions

I originally had a getView () adapter to create views programmatically where only the loaded image onItemClick

in the gridview worked here. I needed more complex views in each cell, so I changed getView () to use XML files and now none of the gridview listeners are working. Going back to programmatic views, getView () works as expected.

So here is my XML for adapter cells:

Note . I used the same extended object types (RecyclingImageView, ImageViewTintState) for the programmatic views so I don't see I don't need code for them.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/containerView"
android:focusable="false"
android:clickable="true"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.whosay.talent.views.RecyclingImageView
    android:id="@+id/gridImage"
    android:layout_width="match_parent"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_height="match_parent"
    app:tint="@color/image_click_state"
    android:scaleType="centerCrop" />

<com.whosay.talent.views.ImageViewTintState
    android:id="@+id/videoIconView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_gravity="center"
    android:src="@drawable/icon_play_img"
    app:tint="@color/image_click_state"
    android:visibility="invisible" />

      

as you can see, I tried the trick onFocusable

that other questions suggested. I've also tried using more rudimentary views such as ImageView

, but it seems that anything with more than one view in the gridview cell will break the item click method. Also, adding onClickListener to individual views in getView

also doesn't work, even after addingclickable:true

And here is my Gridview xml.

<?xml version="1.0" encoding="utf-8"?>

<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="1dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:horizontalSpacing="1dp"
    android:numColumns="3"
    android:stretchMode="columnWidth"
    android:verticalSpacing="1dp" />

      

+3


source to share


2 answers


Having solved this by adding onClickListeners to the elements in getView()

, still puzzled as to why I had to resort to this



+1


source


I think the problem is with these statements:

android:focusable="false"
android:clickable="true"
android:focusableInTouchMode="false"

      

I don't think you need any of them, especially android:clickable="true"

. This operator essentially blocks the click event from propagating to GridView

, which would normally trigger onItemClick(...)

. Since yours FrameLayout

is clickable (and therefore consumes an event) the grid item is not.



Try to remove all of these statements from every tag in your xml. You also need to uninstall OnClickListeners

which you set in getView(...)

as they set the flag clickable

to true.

There is nothing wrong with setting up OnClickListeners

on widgets inside each grid item. But then you cannot use listSelector

that provided visual feedback when clicking on an element. And if you want long print function, you need to install OnLongClickListeners

.

In my opinion, this is additional work that does not need to be done.

+1


source







All Articles