2013-02-08 74 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阻止。

第三行解決您的問題。

如果沒有第一個和第二個,則在彈出窗口外發生任何觸摸/點擊事件時,用戶界面將不會響應。

相關問題