无法在PopupWindow中为GridView执行OnItemClick

时间:2010-01-21 05:59:07

标签: android irc

我有一个gridView,我在弹出窗口中显示(gridview是透明布局,继承自linearlayout,只有部分透明的背景)。我永远无法让这个GridView的OnItemClick执行。当我触摸gridview中的图像时,出现被点击(图像bachgrond更改),但OnItemClick未被调用。

以下是我的Adapter和包含gridView的弹出式视图的代码。谢谢!

//Adapter

public class ImageAdapter扩展BaseAdapter {     private Context mContext;     private int itemBackground;

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

        }
    }); 

3 个答案:

答案 0 :(得分:3)

如果您移除soundSelectorWindow.setTouchable(true);

会发生什么

答案 1 :(得分: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;
        }
    }); 

答案 2 :(得分:1)

检查模拟器的VM大小,使其足以在其VM中运行您的应用程序 检查<ApplicationName.apk>的尺寸。保持最小尺寸,以便模拟器在执行应用程序时不会遇到任何困难。