使用GridView的PopupWindow - 填充视图的麻烦

时间:2009-11-11 23:11:16

标签: android gridview popupwindow

我在尝试使用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)。

任何人都可以向我解释我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

我最终找到了问题。我正在使用ImageAdapter代码 形成Hello,Gallery示例。那包含一行代码 引用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();
      }
 });