无法在 PopupWindow 中执行 GridView 的 OnItemClick

发布于 2024-08-18 06:01:13 字数 2105 浏览 8 评论 0原文

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

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

//Adapter

公共类 ImageAdapter 扩展 BaseAdapter { 私有上下文 mContext; 私有 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();

        }
    }); 

I have a gridView that I display in a popupwindow (the gridview is in a transparent layout, which inherits from linearlayout and just has a partially transparent background). I can never get this GridView's OnItemClick to execute. When I touch an image in my gridview it appears to be clicked (image bachgrond changes), but OnItemClick is not being called.

Below is the code for my Adapter and my popupview containing the gridView. Thanks!

//Adapter

public class ImageAdapter extends 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();

        }
    }); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你如我软肋 2024-08-25 06:01:13

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

what happens if you remove soundSelectorWindow.setTouchable(true);

迷迭香的记忆 2024-08-25 06:01:13

我无法解释为什么 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;
        }
    }); 

I can't explain why the OnItemClickListener isn't working, but when I substituted with OnTouchListener it worked. Do you need to differentiate between which item is clicked or is it enough just to know that the popup was clicked? From you code it looks like the latter, so OnTouch should work:

    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;
        }
    }); 
翻了热茶 2024-08-25 06:01:13

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文