GridView.setOnItemClickListener不起作用

时间:2012-08-13 09:08:07

标签: android gridview android-adapterview

我从2days开始遇到一个问题。我有一个网格视图,我需要显示图像。当我点击网格项时,它必须转到下一个活动。我能够在gridview中显示图像但是事情就是当我点击那个项目它没有响应..(OnItemClickListener不工作)。我无法在我做错的地方追踪我的问题。

         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");

}

       }

这是我的片段..

         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;

}

     } 

这是我的适配器。

如果你们知道的话。请你告诉我......

谢谢, 切塔尼亚

3 个答案:

答案 0 :(得分:24)

我认为你的ImageViews窃取了Focus,因为它们是可检查的。所以项目点击不会发生,因为你的ImageViews拦截它。

将这些属性添加到您的图像视图可能有所帮助,但可能会给您的检查带来麻烦。

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

列表视图中的可检查项目有点痛苦。但我认为你会找到相关的主题如何做到这一点。

这是一个看起来合适的教程,我承认我没有仔细研究它,但你可能想:

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

答案 1 :(得分:2)

还要确保您的适配器为isEnabled

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

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

  

isEnabled(int position)如果指定的项目返回true   position不是分隔符。

否则点击事件不会为您的项目抛出

答案 2 :(得分:1)

希望这可以帮助某人,     在gridview中,如果添加按钮或设置&#34; android:clickable = true&#34;在其中的元素中,不会监听gridview的OnItemClickListener,而是可以创建带有图像的Image或TextView,并查看您没有设置clickable =&#34; true&#34;。现在,OnItemClickListener将监听TextView,这对我有用。