2013-03-06 56 views
17

我正在開發一個android電視應用程序。我有一個listviewImageButton + Textview s作爲孩子。由於電視不需要觸摸事件,但使用D-PAD或軌跡球遙控器,我必須關注所有可點擊的項目,以便用戶知道焦點所在。但問題是,我可以專注於列表行,如果我的列表行中添加Android自定義ListView與ImageButton沒有得到重點

android:descendantFocusability="blocksDescendants"

自定義列表行佈局

ImageButton不會得到焦點。 如果我刪除上面的代碼,然後ImageButton獲得焦點,但我不能單擊列表行。

另外,我加了setItemsCanFocus爲我的ListView但沒有運氣,沒有什麼改變。 我讀了很多文件,但我找不到這個問題的正確答案。我想要關注列表行以及ImageButton。是否有可能,如果任何人知道,請幫我...

回答

20

我必須關注所有可點擊的項目,以便用戶知道焦點在哪裏。但問題是,我可以專注於列表行,如果我添加

查看越來越掛斷由於排文件中的多個視圖,以獲得點擊你要多一個屬性添加到您的行XML的主要佈局。

android:descendantFocusability="blocksDescendants

但是,當我寫上面的屬性,則我不能夠集中的ImageButton這是在我行的文件。

我只是試過一種方法獲得ImageButton以及listview foucus。

要獲得ImageButton的重點:

  1. 創建一個選擇文件。
  2. 應用於Imagebutton。
  3. 向ImageButton添加了android:focusableInTouchMode="true"

要獲取列表視圖的焦點:

  1. 行XML中添加android:descendantFocusability="blocksDescendants主佈局。

現在,當你點擊你的ImageButton時,它將關注你想要的顏色,即使它也會專注於listview點擊。

Selecotor.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    > 
    <item 
     android:state_pressed="true" 
     android:drawable="@android:color/transparent" 
     /> <!-- pressed -->  
    <item 
     android:state_focused="true" 
     android:drawable="@android:color/darker_gray" 
     /> <!-- focused -->  
    <item 
     android:drawable="@android:drawable/btn_star" 
     /> <!-- default --> 
</selector> 

在S3模擬器測試,效果很好。

+0

我做了所有上述的事情,但它不適用於nexus 7模擬器以及電視。我認爲它是因爲沒有電視觸摸屏,所以android:focusableInTouchMode =「true」不起作用 – 2013-04-01 13:56:24

+0

它在nexus 7模擬器中工作也很好。 – RobinHood 2013-04-02 09:15:03

+0

重點是要列出行和按鈕...? – 2013-04-02 09:18:43

1

您是否嘗試過:

setFocusableInTouchMode(false); 

setFocusable(false); 

在這兩個項目(所的ImageButton和TextView的)?

+0

感謝您的時間,但如果用戶不能重點然後用遙控器用戶不知道他是否在正確的ImageButton ... – 2013-03-07 07:19:30

6

我以前有同樣的問題。我解決了我的問題(添加到xml中的佈局)

android:focusableInTouchMode="true" 
android:clickable="true" 

所以我的問題得到了解決。

注意:僅添加android:focusableInTouchMode =「true」不起作用,您必須在focusableInTouchMode爲true後將佈局可點擊爲true。

4

我在listView中有同樣的問題,但我找到了解決方案,只需使用imageView而不是圖像按鈕,那麼行也是可點擊和可聚焦的。

注意:當我們使用listView中的按鈕或圖像按鈕時,列表視圖的行會被禁用以防止發生這種情況,請改用view或imageview。

希望所以這會幫助你。

+0

即使有所有建議的評論,我都無法讓ImageButton工作,但是這很有效。 – bcorso 2014-11-06 19:01:17

3

添加下面的代碼,自定義行的父佈局,

android:descendantFocusability="blocksDescendants" 

和下面的代碼在佈局添加到任何按鈕或ImageButton的

android:focusable="false" 
android:focusableInTouchMode="false"