2012-06-28 104 views
13

我試圖從PopupWindow中顯示一個ListView。但是當我試圖調用ListView的setOnItemClickListener什麼都沒有。在這裏它的Java文件Android ListView的PopupWindow中的setOnItemClickListener不叫

PopupWindowActivity.java

public class PopupWindowActivity extends Activity { 
    String[] data = { "DATA 1", "DATA 2", "DATA 3", "DATA 4", "DATA 5", "DATA 6" }; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.a); 
    final Button btnOpenPopup = (Button) findViewById(R.id.openpopup); 
    btnOpenPopup.setOnClickListener(new Button.OnClickListener() { 

     public void onClick(View arg0) { 
      LayoutInflater layoutInflater = (LayoutInflater) getBaseContext() 
        .getSystemService(LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.main, null); 
      final PopupWindow popupWindow = new PopupWindow(popupView, 
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

      ListView listView = (ListView) popupView.findViewById(R.id.listView1); 
      listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,data)); 
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       public void onItemClick(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        // TODO Auto-generated method stub 
        System.out.println("Item Clicked"); 
        popupWindow.dismiss(); 
       } 
      }); 

      popupWindow.showAsDropDown(btnOpenPopup, 20, -5); 

     } 
    }); 
} 

}

這是第一個XML文件 A.XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 
<Button 
    android:id="@+id/openpopup" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Open Popup Window" /> 

</LinearLayout> 

這誇大xml文件 main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/recording" 
android:orientation="vertical" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="30sp" 
    android:layout_marginLeft="30sp" 
    android:layout_marginRight="30sp" 
    android:layout_marginTop="100sp" > 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="#00000000" > 
    </ListView> 
</RelativeLayout> 

</LinearLayout> 

我在做什麼錯?

感謝

+0

可以嘗試的ListView。setAdapter(new ArrayAdapter (MyBannerActivity.this,?代替getApplicationContext(), –

+0

@ Dheeresh Singh:就這樣,沒有任何事情發生 – Mansi

+0

按鈕點擊事件正在工作,但是listview的項目點擊列表工作不起作用 – rajpara

回答

39

只是一個在你的代碼和BOOOM代碼微小的變化會聽你的列表中單擊事件

final PopupWindow popupWindow = new PopupWindow(popupView, 
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true); 

你忘記提及可聚焦PopupWindow構造設置真正

+1

哦,對,當然Android 。爲什麼我會認爲PopupWindow不像我用過的其他視圖那樣默認是可以關注的。有時候我是個笨蛋。 – samosaris

0

這個幫助你 。

在按鈕ClickListener外部聲明listview和列表onClickListener。

5

有同樣的問題,但對我來說setFocusble(false)被要求(和使用ListPopupWindow是不是在我的情況下,該項目已使用的基礎PopupWindow的功能,包括延伸的很多東西的解決方案)。

如果有人在同樣的情況還有一種解決辦法的基礎上的bug商榷here(職位#9)

主要的想法是,ListView的層次仍然接收觸摸事件,所以我們可以手動觸發onItemClick()

但是,這種方法與真實的ListView的觸摸處理不同(例如,在點擊一排時沒有選擇發光),這一點對我來說目前做得非常好。

如果有人有這個問題更精確的解決方案,請分享。

所以,這裏是完整的Adapter的代碼可與ListView使用內部PopupWindow這是setFocusable(false)

私有類CustomAdapter擴展ArrayAdapter {

private LayoutInflater mInflater; 
private ListView mOwningListView; 

public CustomAdapter(Context context, List<String> objects, ListView listView) { 
    super(context, android.R.layout.simple_list_item_1, objects); 
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mOwningListView = listView; 
} 


@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.font_pick_row, null); 
    } 
    // this is the key point of workaround 
    convertView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      /* 
      * as every row is still receiving their touches 
      * we can use this to manually trigger onItemClick 
      * since it doesn't firing in popupWindow.setFocusable(false) 
      */ 
      mOwningListView.getOnItemClickListener().onItemClick(mOwningListView, v, position, getItemId(position)); 

     } 
    }); 
    //... other stuff 
    return convertView; 
} 

}

+0

無論什麼時候處理鍵盤,這看起來都是更好的答案。 ! –

+0

很高興幫助!如果你認爲這個答案是有用的,所以其他人也可以達到這個解決方案 –

+0

這是答案保持「setFocusble(false)」! – wangqi060934

相關問題