不能在PopupWindow执行OnItemClick的GridView控件 [英] Cannot execute OnItemClick for GridView in PopupWindow

查看:117
本文介绍了不能在PopupWindow执行OnItemClick的GridView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的popupwindow显示GridView控件(GridView的是在一个透明的布局,从继承的LinearLayout,只是有部分透明的背景)。我永远无法得到这个GridView的OnItemClick执行。当我在GridView控件触摸图像它的出现的被点击(图像bachgrond的变化),但OnItemClick不会被调用。

下面是code为我的适配器和含有GridView的我popupview。谢谢!

  //适配器

公共类ImageAdapter延伸BaseAdapter {
    私人语境mContext;
    私人诠释itemBackground;

 公共ImageAdapter(上下文C){
    mContext = C;  // ---设置---风格
    TypedArray A = c.obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
    a.recycle();
}

...

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    ImageView的ImageView的;
    如果(convertView == NULL){
        ImageView的=新ImageView的(mContext);
    }其他{
        ImageView的=(ImageView的)convertView;
    }
    imageView.setImageResource(图像[位置]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageView.setBackgroundResource(itemBackground);
    返回ImageView的;}
公共整数[] =图像{
        R.drawable.sound1,
        R.drawable.sound2,
        R.drawable.sound3,
        R.drawable.sound4
};

}

  //////////在活动,////////的onCreate...最后LayoutInflater吹气=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    最后TransparentLayout musicGrid =(TransparentLayout)inflater.inflate(R.layout.gridviewpopup,空,假);
    最终的GridView gView =(GridView控件)musicGrid.findViewById(R.id.music_gridview);
    gView.setAdapter(新ImageAdapter(本));    最后PopupWindow soundSelectorWindow =新PopupWindow(本);
    soundSelectorWindow.setContentView(musicGrid);
    soundSelectorWindow.setTouchable(真);    gView.setOnItemClickListener(新OnItemClickListener()
    {
        公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的ID)
        {
            //永远不会HERE
            soundSelectorWindow.dismiss();        }
    });


解决方案

如果你碰巧删除 soundSelectorWindow.setTouchable(真);

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

        }
    }); 

解决方案

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

这篇关于不能在PopupWindow执行OnItemClick的GridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆