【问题标题】:setOnItemClickListener for GridView in PopupWindow doesn't workPopupWindow 中 GridView 的 setOnItemClickListener 不起作用
【发布时间】:2013-02-08 16:32:21
【问题描述】:

如果我在正常的 Activity 中有一个 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>

图像适配器

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 的点击并打开弹出窗口。显然只能有一个侦听器,我无法将 onClick 设置为 Preference,所以我不知道如何解决这个问题(onPreferenceClick 永远不会检测到任何点击)。第一个调用弹出窗口的侦听器(我可以获得该点击的唯一方法):

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

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

【问题讨论】:

  • 事件本身是否进入弹出窗口?
  • ITEM SELECTED 是否曾经打印到 logcat?
  • @Nathaniel 弹出窗口运行良好,只是 clicklistener 不行
  • 你能发布 ImageAdapter 的代码吗?您是否在 ImageAdapter 中注册了任何 OnClickListener?
  • 我添加了 ImageAdapter。我那里没有点击监听器

标签: android gridview popupwindow onitemclicklistener


【解决方案1】:

最近遇到这个问题,在这里找到了解决方案

popupWindow.setFocusable(true);

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

【讨论】:

    【解决方案2】:

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

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

    第一行启用弹出窗口外的触摸事件。

    没有第二行的触摸事件将被弹出窗口阻止。

    第三行解决了你的问题。

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

    【讨论】:

      【解决方案3】:

      确保您的 ImageAdapter 正在膨胀的布局没有注册任何 OnItemClickListener 和/或在适配器布局上设置 android:focussable=false。

      如:

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

      【讨论】:

      • 如果您查看 ImageAdapter,我没有使用任何图像布局。我也没有其他点击监听器
      猜你喜欢
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多