2013-02-08 79 views
3

如果我在正常的Activity中有一个GridView,它工作正常。但在这里,我在PopupWindow,我尝试了所有的工作,但onItemClick不会被调用!我试着用下面的,这是对大多数人的解决方案,但他们没有为我工作(也许我已经被滥用呢?):PopupWindow中的GridView的setOnItemClickListener不起作用

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设置为首选项,所以我不知道如何解决此问题(onPreferenceClick从未检测到任何点击)。这就要求弹出(我能得到该次点击的唯一途径)第一个监听:

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

点击在屏幕上显示(我甚至尝试drawSelectorOnTop),但不管如何,单击不会得到通过。

+0

事件是否进入弹出窗口本身? – Nathaniel 2013-02-08 16:55:42

+0

项目选择是否打印到logcat? – NPike 2013-02-08 17:17:39

+0

@NPike不,它不 – 2013-02-08 17:20:06

回答

-1

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

如:

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

如果你看ImageAdapter,我没有使用任何图像布局。我也没有其他clicklisteners – 2013-02-08 19:09:21

0

我已经找到了完美的解决方案对我来说:

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

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

没有第二行触摸事件将被popupwindow阻止。

第三行解决您的问题。

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

相关问题