Asaf Nevo
Asaf Nevo

Reputation: 11698

Android GridView onItemClickListener doesn't fire

This is my GridView ArrayAdapter:

<?xml version="1.0" encoding="utf-8"?>
<!-- gridview adapter layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:background="@drawable/gridview_background_selector" >

    <!-- profile picture layout -->

    <RelativeLayout
        android:id="@+id/fragment_events_adapter_relativelayout_profile_picture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp" >

        <!-- profile picture image view -->

        <ImageView
            android:id="@+id/fragment_events_adapter_imageview_profile_picture"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/app_name"
            android:scaleType="fitXY"
            android:src="@drawable/facebook_blank_profile" />

        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/background_black_transparent" >

            <!-- user name text view -->

            <com.coapps.pico.NillanTextView
                android:id="@+id/fragment_events_adapter_textview_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:src="@drawable/facebook_blank_profile"
                android:textColor="@android:color/white"
                app:isBold="true" />
        </HorizontalScrollView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fragment_events_adapter_relativelayout_profile_picture"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:background="@color/orange" >

        <HorizontalScrollView
            android:id="@+id/fragment_events_adapter_horizontalscrollview_event_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" >

            <!-- event name -->

            <com.coapps.pico.NillanTextView
                android:id="@+id/fragment_events_adapter_textview_event_name"
                style="@style/text_shadow_black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:src="@drawable/facebook_blank_profile"
                android:textAppearance="@android:style/TextAppearance.Small"
                android:textColor="@android:color/white"
                app:isBold="true" />
        </HorizontalScrollView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fragment_events_adapter_horizontalscrollview_event_name"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:orientation="horizontal" >

            <!-- event icon -->

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="15dp"
                android:layout_gravity="right"
                android:layout_margin="2dp"
                android:contentDescription="@string/app_name"
                android:src="@drawable/ic_action_event" />

            <!-- event start time -->

            <com.coapps.pico.NillanTextView
                android:id="@+id/fragment_events_adapter_textview_start_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:src="@drawable/facebook_blank_profile"
                android:textColor="@android:color/black"
                android:textSize="12sp"
                app:isBold="true" />
        </LinearLayout>
    </RelativeLayout>

    <!-- attending event -->

    <ImageView
        android:id="@+id/fragment_events_adapter_imageview_attending_mask"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:background="@color/background_black_transparent"
        android:contentDescription="@string/app_name"
        android:padding="30dp"
        android:src="@drawable/v"
        android:visibility="gone" />

</RelativeLayout>

and i'm setting an onItemClickedListener:

private void setGridView()
{
    //create a new event list
    picoEventsList = new ArrayList<PicoEvent>();
    //create new gridview arrayadapter
    arrayAdapter = new EventDetailsArrayAdapter(getActivity(), picoEventsList);
    //set grid's view empty view
    View emptyView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_events_empty_view, null);     
    gridView.setEmptyView(emptyView);
    gridView.setOnItemClickListener(this);
    gridView.setAdapter(arrayAdapter);

}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {      
    //get clicked event
    PicoEvent event = arrayAdapter.getItem(arg2);
    //create new event details fragment
    EventDetailsFragment eventDetailsFragment = new EventDetailsFragment();
    //add the event to fragment's arguments
    eventDetailsFragment.setArguments(PicoEventCreator.createBundle(event));
    //show the event details fragment
    getMainActivity().showFragment(null, eventDetailsFragment, true, true);

}

but the onItemClick() method doesn't get fired..

I've checked and non of my views in my layout has clickable="true" setted...

Any ideas ?

Upvotes: 0

Views: 916

Answers (2)

Angesh K Chettiar
Angesh K Chettiar

Reputation: 41

Hey guyz finally got a solution...

what we were doing is directly accessing the Layout inside the GridView, so the onItemClickListener finds it confusing to access the item.

So the solution is to apply the onClickListener inside the Adapter (i.e. normally ArrayAdapter)

so what i m trying to say is:

public View getView(int position, View convertView, ViewGroup parent) {

            //Here row is a view and we can set OnClickListener on this
    final View row;
    ViewHolder holder = null;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        //Here we inflate the layout to view (linear in my case)
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.image = (ImageView) row.findViewById(R.id.image);
        row.setTag(holder);
    } else {
        row = convertView;
        holder = (ViewHolder) row.getTag();
    }

    ImageItem item = data.get(position);
    holder.imageTitle.setText(item.getTitle());
    holder.image.setImageBitmap(item.getImage());

    //Now get the id or whatever needed
    row.setId(position);
    // Now set the onClickListener
    row.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Clicked" + row.getId() + "!!",
                    Toast.LENGTH_SHORT).show();
        }
    });
    return row;
}

Upvotes: 1

Asaf Nevo
Asaf Nevo

Reputation: 11698

Ok i found what the problem was

Apparently GridView and HorzonitalScrollView are not such good friends...

probably because the HorzonitalScrollView has onClick funcionality..

The solution i found is to put:

android:descendantFocusability="blocksDescendants"

in my ArrayAdapter root view.

Upvotes: 0

Related Questions