setOnItemClickListener() not working on gridView
Asked Answered
R

3

6

I know this is already been asked, but somehow the solutions do not work for me.

I have a gridView which is inflated by a relativeLayout. The adapter sets perfectly, When I add a clickListener to one of the childs of relativeLayout they also work fine. But not the itemClickListener on the gridview.

Here is what I have tried:

Gridview:

<GridView
            android:id="@+id/gridView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="true"
            android:clipToPadding="true"
            android:columnWidth="170dp"
            android:fitsSystemWindows="true"
            android:focusable="true"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth" >
</GridView>

Relativelayout added in gridview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlImage"
    android:layout_width="220dp"
    android:layout_height="180dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:paddingBottom="7dp"
    android:paddingRight="7dp" >

    <ImageButton
        android:id="@+id/image"
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:scaleType="centerCrop"
        android:src="@drawable/noimage" />

    <!-- <ImageButton -->
    <!-- android:id="@+id/imageHover" -->
    <!-- android:layout_width="220dp" -->
    <!-- android:layout_height="220dp" -->
    <!-- android:adjustViewBounds="true" -->
    <!-- android:background="@null" -->
    <!-- android:scaleType="fitXY" -->
    <!-- android:src="@drawable/tile_selector_style" /> -->

    <TextView
        android:id="@+id/ShowTitle"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:hint="Show Title"
        android:paddingBottom="40dp"
        android:paddingRight="20dp"
        android:singleLine="true" />

    <TextView
        android:id="@+id/ShowTime"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:hint="Show Time"
        android:paddingBottom="20dp"
        android:paddingRight="20dp"
        android:singleLine="true" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="0dp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</RelativeLayout>

The setting of gridview listener:

gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Log.v("griditem", "clicked");
                Toast.makeText(context, "Here", Toast.LENGTH_LONG).show();

            }
        });

No idea whats going wrong here..

Remanence answered 29/4, 2014 at 11:20 Comment(7)
I think you should use linearLayout.gv.setOnItemClickListenerFingering
try to remove this properties from your grid item android:focusable="false" android:focusableInTouchMode="false"Bohemian
@Kedarnath my gv is: public static GridView gv; gv = (GridView) getActivity().findViewById(R.id.gridView);Remanence
ok try to set this android:descendantFocusability="blocksDescendants" to your RelativeLayout and remove android:focusable="false" android:focusableInTouchMode="false" from every childPhysiognomy
@Haresh from all the items inside the relativelayout?Remanence
yes and also from relative layout too.Bohemian
@SimplePlan it is working now. Thanks. Can you post it as an answer?Remanence
P
20

try to set android:descendantFocusability="blocksDescendants" to your RelativeLayout

and remove android:focusable="false" android:focusableInTouchMode="false" from every child

Physiognomy answered 29/4, 2014 at 11:38 Comment(0)
T
15

Hey folks this is how i solved the problem;

While using clickable objects in the grid then unfortunately android can not handle the click event of grid.

SO USE use interface view.

Example: USE IMAGEVIEW INSTEAD OF IMAGEBUTTON AND BUTTON

if you need to show some text in that image use textview and align that on top of imageview.

Telephonic answered 28/3, 2015 at 4:45 Comment(0)
S
0

I solved this issue by adding onitemclicklistener in my gridviewadapter itself(inside getview method).

I don't know whether this practice is right or wrong but it worked for me.

Serranid answered 1/1, 2022 at 11:48 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewDepository

© 2022 - 2024 — McMap. All rights reserved.