本文介绍了GridView.setOnItemClickListener不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直患有有一个问题,因为2days.I有一个网格视图中,我需要显示images.When我点击网格项它要到明年activity.I我能够在GridView中显示图像,但事情是,当我点击这个项目是不响应。(OnItemClickListener不工作)。我不能能够追踪我的问题,我做错了。

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

}

       }

这是我的片段。

         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;

}

     }

这是我的适配器。

如果你们know.Canü请告诉我...

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

谢谢,切塔尼亚

推荐答案

我觉得你ImageViews偷焦点,因为它们是可检查的。因此,该项目点击犯规发生,因为你的ImageViews拦截它。

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

将这些属性添加到您的imageviews可能有帮助,但也许可以麻烦你checkings。

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

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

具有可检查的项目在ListView是痛苦位。但我想你会发现相关主题如何做到这一点。

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

下面是1教程,这似乎合适的,我承认我没有它扔仔细一看,但你可能想:

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

这篇关于GridView.setOnItemClickListener不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 17:13