2011-04-06 73 views
0

我已經在谷歌的Android開發者論壇上發佈了相同的問題。 我在這裏重複:ListViews裏面TableLayout

我有一個TableLayout與一個TableRow。 第一列/單元格是一個ListView。第二個單元格/列是由2個ListViews組成的 TableLayout。

當我膨脹的XML佈局,一切看起來不錯,我已經爲每個3個ListView設置了 適配器。

我已經通過適配器將虛擬元素添加到這些列表中,並且能夠在列表中上下滾動。

但onItemClick沒有被觸發。

以下是我的佈局文件 -

<RelativeLayout android:id="@+id/top_header" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <Button 
        android:text="Edit" 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 
      </Button> 
      <TextView 
        android:text="dummy1" 
        android:textStyle="bold" 
        android:id="@+id/textView3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@+id/button1" 
        android:layout_centerVertical="true" 
        android:paddingLeft="25dip" 
        android:textSize="18dip"> 
      </TextView> 
      <Button 
        android:id="@+id/button2" 
        android:layout_alignParentTop="true" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="Cancel" 
        android:layout_alignParentRight="true"> 
      </Button> 
      <TextView 
        android:layout_alignParentTop="true" 
        android:text="dummy2" 
        android:textStyle="bold" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:id="@+id/textView4" 
        android:layout_toLeftOf="@+id/button2" 
        android:layout_centerVertical="true" 
        android:paddingRight="25dip" 
        android:textSize="18dip"> 
      </TextView> 
    </RelativeLayout> 

    <TableLayout android:id="@+id/tableLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:shrinkColumns="1"> 

     <TableRow android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:divider="@android:color/black" 
      android:dividerPadding="2dip"> 
      <ListView 
        android:id="@+id/listView1" 
        android:layout_height="wrap_content" 
        android:layout_width="0dip" 
        android:layout_weight="1" 
        android:clickable="true" 
        android:focusable="true" 
        > 
      </ListView> 
      <TableLayout android:id="@+id/tableLayout2" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:shrinkColumns="1"> 
       <TextView 
        android:text="dummy3" 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textStyle="bold" 
        android:padding="8dip" 
        android:textSize="18dip" 
        android:background="#ff238E23"> 
       </TextView> 
       <ListView 
        android:id="@+id/listView2" 
        android:layout_height="0dip" 
        android:layout_weight="1" 
          android:drawSelectorOnTop="false" 
          android:divider="@android:color/white" 
          android:dividerHeight="2dip" 
        android:layout_width="match_parent" 
          android:clickable="true" 
          android:focusable="true" 
        > 
       </ListView> 
       <TextView 
        android:text="dummy4" 
        android:textSize="18dip" 
        android:textStyle="bold" 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="#ff0000ff" 
        android:padding="8dip"> 
       </TextView> 
       <ListView 
        android:id="@+id/listView3" 
        android:layout_height="0dip" 
        android:layout_weight="1" 
          android:drawSelectorOnTop="false" 
          android:divider="@android:color/white" 
          android:dividerHeight="2dip" 
        android:layout_width="match_parent" 
          android:clickable="true" 
          android:focusable="true" 
        > 
       </ListView> 
      </TableLayout> 
     </TableRow> 

    </TableLayout> 

並在Java文件,我做

ListView lv = (ListView) findViewById(R.id.listView1); 
    lv.setAdapter(new EfficientAdapterLeft(this)); 
    lv.setOnItemClickListener(new OnItemClickListener() 

{ @覆蓋 公共無效onItemClick( AdapterView arg0,查看視圖,int arg2, long arg3)Log.v(「blah」,「arg2」+ arg2); Log.v(「blah」,「arg3」+ arg3); } });

我爲剩餘的兩個列表R.id.listView2和 R.id.listView4做類似的事情。他們實例化不同的適配器,但沒有任何 截然不同。

我沒有看到打印日誌。爲什麼我無法點擊 項目並讓onClickItem偵聽器被觸發?即使這樣做 lv.setFocusable(true); lv.setClickable(true); 沒有幫助。

任何想法發生了什麼?

謝謝。

+0

我應該問在下面回答之前,你是否填充了你的listviews內容?我想我問的是,當你嘗試點擊它們時,你能看到列表視圖中的任何內容嗎? – 2011-04-06 00:30:17

回答

1

可能是因爲ListView元素甚至不可見,或者它的高度爲零。如果ListViews沒有被填充,那麼高度(因爲wrap_content)將爲零,使得它們無法點擊。作爲測試,手動將高度設置爲25sp,看看它是否有所作爲。

編輯:您必須在適配器內設置onclicklistener。現在,適配器填充的項目有焦點,而不是整體列表視圖。以this爲例。

+0

可能的另一件事是,如果您正在填充內容,那麼使用適配器實例化的clicklisteners會抓取點擊事件,將它們傳遞給填充的項目而不是整個列表視圖。 – 2011-04-06 00:31:04

+0

我正在填充列表視圖。我可以在屏幕上看到內容。我甚至可以查看列表視圖。但是當我點擊列表項目時,它們既不會突出顯示,也不會觸發onClick監聽器。 – 2011-04-06 01:46:37

+0

我編輯了我的答案,看看它是否解決了這個問題。 – 2011-04-06 15:18:42