faizanjehangir
faizanjehangir

Reputation: 2851

setOnItemClickListener() not working on gridView

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

Upvotes: 6

Views: 8109

Answers (3)

Vijay Gupta
Vijay Gupta

Reputation: 1

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.

Upvotes: 0

manDroid
manDroid

Reputation: 1135

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.

Upvotes: 15

M D
M D

Reputation: 47817

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

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

Upvotes: 20

Related Questions