PopupWindow中的GridView的setOnItemClickListener不起作用

时间:2013-02-08 16:32:21

标签: android gridview popupwindow onitemclicklistener

如果我在普通活动中有 GridView ,它可以正常工作。但是我在 PopupWindow 中有它,我尝试了一切来使它工作,但onItemClick不会被调用!我试过以下,这是大多数人的解决方案,但他们不适合我(也许我误用了他们?):

android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"

活动的一部分:

bgGrid.setAdapter(new ImageAdapter(context));
bgGrid.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
        Log.v("CLICK", "ITEM SELECTED " + position);
    }
});
pw = new PopupWindow(layout, posx, posy);
pw.setOutsideTouchable(false);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0); //bgGrid is part of layout

和xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#444444"
    android:orientation="vertical"
    android:padding="5dip" >
<GridView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:columnWidth="160dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" >
        </GridView>
    <Button
        android:id="@+id/popup_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="closePopup"
        android:text="Close" />
</LinearLayout>

ImageAdapter

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }
    public int getCount() {
        return mThumbIds.length;
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(160, 120));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);

        return imageView;
    }

整个弹出窗口部分位于PereferenceActivity中,我有一个侦听器,可以检测到Preference上的单击并打开Popup。显然只能有一个监听器,我无法设置onClick to Preference,所以我不知道如何解决这个问题(onPreferenceClick从不检测任何点击)。调用弹出窗口的第一个侦听器(我可以获得该单击的唯一方法):

images = (Preference) findPreference("prefs_bg");
images.setOnPreferenceClickListener(new BackgroundColorSelector());

点击显示在屏幕上(我甚至尝试过drawSelectorOnTop),但无论如何,点击都无法通过。

3 个答案:

答案 0 :(得分:0)

最近遇到了这个问题并在此处找到了解决方案

popupWindow.setFocusable(真);

解决方案的重复问题: Cannot execute OnItemClick for GridView in PopupWindow

答案 1 :(得分:0)

我找到了一个完美的解决方案:

popUpWindow.setOutsideTouchable(true);
popUpWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color.white));
popUpWindow.setFocusable(true);

第一行启用popupwindow外的触摸事件。

如果没有第二行,弹出窗口将阻止触摸事件。

第三行解决了你的问题。

如果没有第一个和第二个,当弹出窗口外发生时,用户界面将不会响应任何触摸/点击事件。

答案 2 :(得分:-1)

确保ImageAdapter正在充气的布局没有注册任何OnItemClickListeners和/或在适配器布局上设置了android:focussable = false。

如:

<ImageView
android:id="@+id/imageViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:focussable=false 
/>