Android GridView自定义BaseAdapter ImageView ImageButton OnItemClick不起作用

时间:2011-05-24 15:34:08

标签: android

使用GridView和CustomAdapter(扩展BaseAdapter)时遇到问题。

  • 我的Activity实现了OnItemClickListener。
  • 如果我使用ImageView作为项目一切正常,OnItemClick-Events将被触发/捕获

我没有找到一个使用ImageButton的自定义BaseAdapter的GridView的有用示例。 有人有想法吗?

代码段:

class MyActivity extends Activity implements OnItemClickListener {
  ...
  @Override
  public void onCreate() {
    ...
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setOnItemClickListener(this);
    gridview.setAdapter(new ImageButtonAdapter(this));
  }
  ...    
  @Override
  public void onItemClick(AdapterView<?> adapter, View view, int arg2, long arg3)
  {
    Log.e("onItemClick()", "arg2=" + arg2 + ", arg3=" + arg3);
  }
}


public class ImageButtonAdapter extends BaseAdapter
{
  private Context    mContext;

  public LayoutMenuAdapter(Context c)
  {
    mContext = c;
  }

  public int getCount()
  {
    return mThumbIds.length;
  }

  public Object getItem(int position)
  {
    return null;
  }

  public long getItemId(int position)
  {
    return 0;
  }

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

    /* IF I USE THIS PART EVERYTHING WORKS FINE */

    // ImageView imageView;
    // if (convertView == null) {
    //    imageView = new ImageView(mContext);
    //    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
    //    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    //    imageView.setPadding(0, 0, 0, 0);
    //    imageView.setFocusable(false);
    //  } else {
    //    imageView = (ImageView) convertView;
    //  }
    //  imageView.setImageResource(mThumbIds[position]);
    //  return imageView;

    /* IF I USE THIS PART NO THE ACTIVITY/LISTENER RECEIVES NO EVENT */

    ImageButton imageButton;
    if (convertView == null) {
      imageButton = new ImageButton(mContext);
      imageButton.setLayoutParams(new GridView.LayoutParams(100, 100));
      imageButton.setScaleType(ImageView.ScaleType.CENTER_CROP);
      imageButton.setPadding(0, 0, 0, 0);
      imageButton.setFocusable(false);
    } else {
      imageButton = (ImageButton) convertView;
    }
    imageButton.setImageResource(mThumbIds[position]);
    return imageButton;
  }

  // references to images
  private Integer[] mThumbIds = { R.drawable.media};
}

2 个答案:

答案 0 :(得分:1)

在getView().....中实现OnClickListener

Class MyActivity extends Activity implements OnItemClickListener {
      ...
      @Override
      public void onCreate() {
        ...
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setOnItemClickListener(this);
        gridview.setAdapter(new ImageButtonAdapter(this));
      }
      ...    
     // @Override
     // public void onItemClick(AdapterView<?> adapter, View view, int arg2, long arg3)
     // {
      //  Log.e("onItemClick()", "arg2=" + arg2 + ", arg3=" + arg3);
     // }
    }


    public class ImageButtonAdapter extends BaseAdapter
    {
      private Context    mContext;

      public LayoutMenuAdapter(Context c)
      {
        mContext = c;
      }

      public int getCount()
      {
        return mThumbIds.length;
      }

      public Object getItem(int position)
      {
        return null;
      }

      public long getItemId(int position)
      {
        return 0;
      }

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

        /* IF I USE THIS PART EVERYTHING WORKS FINE */

        // ImageView imageView;
        // if (convertView == null) {
        //    imageView = new ImageView(mContext);
        //    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
        //    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //    imageView.setPadding(0, 0, 0, 0);
        //    imageView.setFocusable(false);
        //  } else {
        //    imageView = (ImageView) convertView;
        //  }
        //  imageView.setImageResource(mThumbIds[position]);
        //  return imageView;

        /* IF I USE THIS PART NO THE ACTIVITY/LISTENER RECEIVES NO EVENT */

        ImageButton imageButton;
        if (convertView == null) {
          imageButton = new ImageButton(mContext);
          imageButton.setLayoutParams(new GridView.LayoutParams(100, 100));
          imageButton.setScaleType(ImageView.ScaleType.CENTER_CROP);
          imageButton.setPadding(0, 0, 0, 0);
          imageButton.setFocusable(false);
        } else {
          imageButton = (ImageButton) convertView;
        }
        imageButton.setImageResource(mThumbIds[position]);
        imageButton.setOnClickListener(new OnClickListener(){

             public void onClick(View arg0) {

                 Log.e("onClick()", position);

        }

         });
        return imageButton;
      }

      // references to images
      private Integer[] mThumbIds = { R.drawable.media};
    }

答案 1 :(得分:1)

非常感谢您的帮助,现在我的Activity只实现了一个OnClickListener,它将通过ctor传递给Adapter并绑定到每个按钮:

class MyActivity extends Activity implements OnClickListener {
  ...
  @Override
  public void onCreate() {
    ...
    GridView gridview = (GridView) findViewById(R.id.gridview);
    // gridview.setOnItemClickListener(this); // removed
    gridview.setAdapter(new ImageButtonAdapter(this, this));
  }
  ...    
  @Override
  public void onClick(View view)
  {
    Log.i("onClick()", "view=" + view);
  }
}


public class ImageButtonAdapter extends BaseAdapter
{
  private Context    mContext;

  private OnClickListener mListener;

  public LayoutMenuAdapter(Context c, OnClickListener l)
  {
    mContext = c;
    mListener = l;
  }

  public int getCount()
  {
    return mThumbIds.length;
  }

  public Object getItem(int position)
  {
    return null;
  }

  public long getItemId(int position)
  {
    return 0;
  }

  public View getView(int position, View convertView, ViewGroup parent)
  {
    ImageButton imageButton;
    if (convertView == null) {
      imageButton = new ImageButton(mContext);
      imageButton.setLayoutParams(new GridView.LayoutParams(100, 100));
      imageButton.setScaleType(ImageView.ScaleType.CENTER_CROP);
      imageButton.setPadding(0, 0, 0, 0);
      imageButton.setFocusable(false);
    } else {
      imageButton = (ImageButton) convertView;
    }
    imageButton.setImageResource(mThumbIds[position]);
    imageButton.setOnClickListener(mListener);
    return imageButton;
  }

  // references to images
  private Integer[] mThumbIds = { R.drawable.media};
}