6

I have been suffering with one problem since 2days.I have a grid view in that i need to display images.When I click on grid item it has to go to next activity.I am able to display images in gridview but the thing is when I click on that item it is not responding..(OnItemClickListener is not working).I couldn't able to trace my problem where I have done wrong.

         package com.logictreeit.mobilezop.fragments;

     import android.app.Activity;
     import android.content.Context;
     import android.os.Bundle;
     import android.support.v4.app.Fragment;
     import android.util.Log;
     import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
     import android.widget.AdapterView;
     import android.widget.AdapterView.OnItemClickListener;
     import android.widget.GridView;

     import com.logictreeit.mobilezop.adapters.PhotoAdapter;
     import com.logictreeit.mobilezop.custom.Utils;

      public class Dup_AlbumPhotosFragment extends Fragment implements
                OnItemClickListener {

private static final String TAG = "AlbumPhotos Fragment";
private GridView gridView;
private Context mContext;
private PhotoAdapter photoAdapter;

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.v(TAG, "on Activity Created ");

}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.mContext = activity;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.v(TAG, "OnCreateView");
    gridView = new GridView(mContext);
    gridView.setNumColumns(GridView.AUTO_FIT);
    gridView.setClickable(true);
    gridView.setOnItemClickListener(this);
    photoAdapter = new PhotoAdapter(mContext,                   -1,Utils.getALbumList().get(0).getPhotosList());
    gridView.setAdapter(photoAdapter);
    return gridView;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    Log.v(TAG, "on ItemClikced");

}

       }

This is my Fragment..

         package com.logictreeit.mobilezop.adapters;

        import java.util.List;

         import android.content.Context;
         import android.view.LayoutInflater;
         import android.view.View;
         import android.view.ViewGroup;
         import android.widget.ArrayAdapter;
         import android.widget.CheckBox;
         import android.widget.CompoundButton;
         import android.widget.CompoundButton.OnCheckedChangeListener;
         import android.widget.ImageView;

        import com.logictreeit.mobilezop.R;
        import com.logictreeit.mobilezop.models.Photo;

            public class DupPhotoAdapter extends ArrayAdapter<Photo> {
            private static final String TAG = "PhotoAdapter";
            private Context context;
private List<Photo> photoList;

public DupPhotoAdapter(Context context, int textViewResourceId,
        List<Photo> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    this.photoList = objects;
}

public int getCount() {
    return photoList.size();
}

public View getView(int position, View convertView, ViewGroup parent) {
    convertView = LayoutInflater.from(context).inflate(
            R.layout.grid_item_image_layout, null);

    ImageView imageView = (ImageView) convertView
            .findViewById(R.id.grid_item_imageview);
    final CheckBox checkBox = (CheckBox) convertView
            .findViewById(R.id.grid_item_checkbox);
    final Photo photo = photoList.get(position);

    if (photo.isSelected()) {
        checkBox.setChecked(true);
    } else {
        checkBox.setChecked(false);
    }
    imageView.setImageResource(Integer.parseInt(photo.getFileUrl()));
    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                photo.setSelected(true);
            } else {
                photo.setSelected(false);
            }

        }
    });
    return convertView;

}

     } 

This is my Adapter.

If you guys know.Can u please tell me...

Thanks, Chaitanya

Chaitu
  • 907
  • 2
  • 13
  • 27

3 Answers3

25

I think your ImageViews stealing Focus because they are Checkable. So the item click doesnt happen because your ImageViews intercepting it.

Adding these attributes to your imageviews might help but probably could trouble your checkings.

    android:focusable="false"
    android:focusableInTouchMode="false"

Having checkable items in a listview is bit of pain. But i think you will find related topics how to do it.

Here is 1 tutorial that seems suitable, i admit i didnt threw a closer look on it but you might want to:

http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php

Ostkontentitan
  • 6,930
  • 5
  • 53
  • 71
  • 3
    Thanks Konstantin..Thank you so much..It worked for me..Added those props for both imageview and checkbox then only it is working. – Chaitu Aug 13 '12 at 10:15
  • I have added the image view and checkbox in a linear layout programmatically. So I'm doing the above 2 lines through java code. But, still I'm not able to receive any click items. – Namratha Feb 05 '13 at 09:33
  • I have a grid view and each item is a linear layout, the click event in this case is not received but if the item is an image, then the click event is received any idea how to resolve this? I have tried doing this but it doesn't work: imageView.setClickable(false); imageView.setFocusable(false); imageView.setFocusableInTouchMode(false); linearLayout.setClickable(true); linearLayout.setFocusable(true); linearLayout.setFocusableInTouchMode(true); – Namratha Feb 05 '13 at 09:52
  • 1
    In my case, the root view of my gridview item should also have `android:focusable="false"` and `android:focusableInTouchMode="false"`. Thank you very much! –  Sep 29 '15 at 02:02
2

Also make sure, that your Adapter returns true for isEnabled

@Override
     public boolean isEnabled(int i) {
     return true;
}

http://developer.android.com/reference/android/widget/BaseAdapter.html#isEnabled(int)

isEnabled(int position) Returns true if the item at the specified position is not a separator.

Otherwise the click event, will not be throw for your item

Renaud Boulard
  • 709
  • 6
  • 9
1

Hope this may help someone, In gridview, if you add buttons or set "android:clickable=true" in the element inside it, gridview's OnItemClickListener will not be listened, instead you can create an Image or TextView with image and see to that you have not set the clickable="true". Now, the OnItemClickListener will listen to the TextView and this worked for me.

Learner_Programmer
  • 1,259
  • 1
  • 13
  • 38