2012-12-31 44 views
0

這是我的問題:ListView OnClickListener

我有一個chatapplication和消息顯示在一個ListView中。 ListView填充屏幕的特定部分。 如果用戶單擊ListView,則應顯示輸入對話框。我的問題是,我只能用onItemClickListener識別ListView上的點擊,但是當應用程序啓動時,沒有項目可以在ListView中單擊。

我想到了在ListView控件按鈕:

<FrameLayout 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
android:layout_marginTop="10dip" 
> 

    <ListView 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:id="@+id/ver_list" 
    android:stackFromBottom="true" 
    android:cacheColorHint="#00000000" 
    android:transcriptMode="alwaysScroll"> 
    </ListView> 

    <Button 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:id="@+id/ver_listbutton" 
    android:background="@null"></Button> </FrameLayout> 

現在我可以承認在ListView控件與在它的按鍵空間點擊。

但隨着FrameLayout我無法滾動ListView,因爲它在按鈕下方。

有沒有人有解決方案?

回答

3

您可以設置按鈕,通過ListView的空視圖,

public void setEmptyView(View emptyView) 

當列表視圖是空的,空視圖將顯示與處理click事件,當有列表視圖項,外置熱交換器空視圖會dispear,然後listview可以處理點擊事件。

+0

好吧,這對於空的ListView效果很好。但是,我怎麼能處理在ListView的區域點擊,如果ListView控件具有entrys呢?一個完整的ListView我可以使用onItemClickListener,但是ListView上面有空間,當它沒有滿的時候,我怎樣才能處理這個空閒空間的點擊? – Gingerbread123321

+0

如果listview有條目,空視圖就會消失,你可以處理點擊事件onItemClickListener如果列表視圖條目不填滿一個屏幕,你可以添加一個透明視圖作爲listview的頁腳視圖,並讓腳視圖處理點擊事件,你需要計算的高度在這種情況下手動透明視圖。 –

+0

好的謝謝你的幫助。我用這個計算了listView的高度:[link](http://iserveandroid.blogspot.de/2011/06/how-to-calculate-lsitviews-total.html)。然後我動態計算了我的標題視圖的高度。 – Gingerbread123321

-1

listView.setOnTouchListener(新OnTouchListener(){

 @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      ListView list = (ListView) v; 
      View child = list.getChildAt(list.getLastVisiblePosition()); 
      if (child != null) { 
       int[] coords = new int[2]; 
       child.getLocationOnScreen(coords); 
       if (event.getRawY() > coords[1] + child.getHeight()+20) { 
        //DO YOU STUFF 
        Toast.makeText(activity, "Outsize", Toast.LENGTH_SHORT).show(); 
        return true;//you have handled the click, listview will not handle it 
       } 
      } 

      return false; // listView handles click 
     } 
    }); 
相關問題