带有 GridView 的 PopupWindow - 膨胀视图的问题

发布于 2024-08-10 11:02:16 字数 890 浏览 5 评论 0原文

我在尝试在 GridView 中使用 GridView 时遇到问题 弹出窗口。在我的 Activity 的 onCreate 方法中,我正在膨胀 xml 中的 gridview 如下:

LayoutInflater inflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
final GridView popupview = (GridView) inflater.inflate
(R.layout.gridviewpopup, null, false);
popupview.setAdapter(new ImageAdapter(this));

希望此 GridView 在单击按钮时弹出。还有里面的 我的活动是 onCreate,我有:

final Button addButton = (Button) findViewById(R.id.add);
 addButton.setOnClickListener(new View.OnClickListener() {  
 public void onClick(View v) {  
     PopupWindow mwindow = new PopupWindow(popupview, 100, 100);
     mwindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 100, 100);
     mwindow.setFocusable(true);  
            }  
 }); 

单击按钮时,我抛出 ClassCastException GridView.onMeasure(int, int)。

谁能向我解释一下我做错了什么?

I am having an issue while trying to use a GridView in a
PopupWindow. On my Activity's onCreate method, I am inflating a
gridview from xml as follows:

LayoutInflater inflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
final GridView popupview = (GridView) inflater.inflate
(R.layout.gridviewpopup, null, false);
popupview.setAdapter(new ImageAdapter(this));

would like this GridView to popup on a button click. Also inside of
my activity's onCreate, I have:

final Button addButton = (Button) findViewById(R.id.add);
 addButton.setOnClickListener(new View.OnClickListener() {  
 public void onClick(View v) {  
     PopupWindow mwindow = new PopupWindow(popupview, 100, 100);
     mwindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 100, 100);
     mwindow.setFocusable(true);  
            }  
 }); 

On button click, I am throwing a ClassCastException from
GridView.onMeasure(int, int).

Can anyone please explain to me what I am doing wrong?

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

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

发布评论

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

评论(1

开始看清了 2024-08-17 11:02:16

我最终找到了问题所在。我正在使用 ImageAdapter 代码
形成“你好,画廊”示例。其中包含一行代码
引用 Gallary:

imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));

与 GridView 一起使用时,这显然会导致类转换
例外。

现在我的 GridView 在弹出窗口中正确显示,我
捕获 OnItemClick 事件时遇到问题。下面是我的代码。
当我在我的中进行选择时,OnItemClick 永远不会被调用
弹出窗口中的 gridview。有什么想法吗?

final GridView gView = (GridView) grid_layout.findViewById
(R.id.gridview_layout);
gView.setWillNotDraw(false);
gView.setFocusableInTouchMode(true);
gView.setClickable(true);
gView.setAdapter(new ImageAdapter(this));

final PopupWindow soundSelectorWindow = new PopupWindow(this);
soundSelectorWindow.setContentView(grid_layout);
soundSelectorWindow.setBackgroundDrawable(new BitmapDrawable());
soundSelectorWindow.setOutsideTouchable(false);
soundSelectorWindow.setTouchable(true);

gView.setOnItemClickListener(new OnItemClickListener()
    {
     public void onItemClick(AdapterView parent, View v, int position,
long id)
      {
       //Never gets here.
       soundSelectorWindow.dismiss();
      }
 }); 

I ended up finding the problem. I was using the ImageAdapter code
form the Hello, Gallery example. That contained a line of code
referencing a Gallary:

imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));

When used with a GridView, this obviously causes a class cast
exception.

Now that I have my GridView displaying properly in a popupwindow, I am
having trouble capturing the OnItemClick event. Below is my code.
OnItemClick is never being called when I make a selection in my
gridview in the popupwindow. Any ideas?

final GridView gView = (GridView) grid_layout.findViewById
(R.id.gridview_layout);
gView.setWillNotDraw(false);
gView.setFocusableInTouchMode(true);
gView.setClickable(true);
gView.setAdapter(new ImageAdapter(this));

final PopupWindow soundSelectorWindow = new PopupWindow(this);
soundSelectorWindow.setContentView(grid_layout);
soundSelectorWindow.setBackgroundDrawable(new BitmapDrawable());
soundSelectorWindow.setOutsideTouchable(false);
soundSelectorWindow.setTouchable(true);

gView.setOnItemClickListener(new OnItemClickListener()
    {
     public void onItemClick(AdapterView parent, View v, int position,
long id)
      {
       //Never gets here.
       soundSelectorWindow.dismiss();
      }
 }); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文