3

我有一个显示在弹出窗口中的 gridView(gridview 处于透明布局中,它继承自 linearlayout 并且只有部分透明的背景)。我永远无法执行此 GridView 的 OnItemClick。当我在 gridview 中触摸图像时,它似乎被单击(图像 bachgrond 更改),但未调用 OnItemClick。

下面是我的适配器和包含 gridView 的弹出视图的代码。谢谢!

//Adapter

公共类 ImageAdapter 扩展 BaseAdapter { 私有上下文 mContext; 私人 int 项目背景;

public ImageAdapter(Context c) {
    mContext = c;

  //---setting the style---
    TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
    a.recycle();
}

……

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(images[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageView.setBackgroundResource(itemBackground);
    return imageView;

}
public Integer[] images = {
        R.drawable.sound1,
        R.drawable.sound2,
        R.drawable.sound3,
        R.drawable.sound4
};

}

//////////In Activity, onCreate////////

...

final LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final TransparentLayout musicGrid = (TransparentLayout) inflater.inflate(R.layout.gridviewpopup, null, false);
    final GridView gView = (GridView) musicGrid.findViewById(R.id.music_gridview);
    gView.setAdapter(new ImageAdapter(this));

    final PopupWindow soundSelectorWindow = new PopupWindow(this);
    soundSelectorWindow.setContentView(musicGrid);
    soundSelectorWindow.setTouchable(true);

    gView.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
        {   
            //NEVER GETS HERE
            soundSelectorWindow.dismiss();

        }
    }); 
4

3 回答 3

3

如果你删除会发生什么 soundSelectorWindow.setTouchable(true);

于 2010-01-21T06:28:51.283 回答
3

我无法解释为什么 OnItemClickListener 不起作用,但是当我用 OnTouchListener 替换它时它起作用了。您是否需要区分单击了哪个项目,或者仅知道单击了弹出窗口就足够了?从您的代码来看,它看起来像后者,所以 OnTouch 应该可以工作:

    popup.setTouchable(true);
    popup.setFocusable(true);
    gView.setClickable(true);

    gView.setOnTouchListener(new View.OnTouchListener() 
    {
        public boolean onTouch(View v, MotionEvent event) 
        {   
            popup.dismiss();
            return true;
        }
    }); 
于 2010-01-21T13:57:49.477 回答
1

Check the VM size of the Emulator so that its good enough to run your app in its VM.
Check the Size of your <ApplicationName.apk>. Keep it in min sized so that Emulator will not find any difficulties in executing your app.

于 2011-10-19T18:18:51.803 回答