2011-01-19 59 views
13

已經閱讀了一些相關的問題在這裏SO,以及通過Android文檔和源查看嘗試找出這一點,但我很難過,但考慮到listSelector似乎只對選定的項目適用的風格,我不會感到震驚......Android ListView狀態列表不顯示默認項目背景

我在這裏main.xml中定義的列表視圖:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fadingEdgeLength="5dp" 
    android:divider="#000000" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/list_selector" /> 
    <TextView 
    android:id="@android:id/empty" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:singleLine="false" 
     android:text="@string/message_list_empty" /> 
</FrameLayout> 

引用的listSelector是在這裏(從很大程度上借用在SDK中找到默認的Android狀態列表:/android/platforms/android-8/data/res/drawable/list_selector_background.xml):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- the window has lost focus, disable the items --> 
    <item 
     android:state_window_focused="false" 
     android:drawable="@drawable/shape_row_disabled" /> 

    <!-- the list items are disabled --> 
    <item 
     android:state_enabled="false" 
     android:state_focused="true" 
     android:state_pressed="true" 
     android:drawable="@drawable/shape_row_disabled" /> 
    <item 
     android:state_enabled="false" 
     android:state_focused="true" 
     android:drawable="@drawable/shape_row_disabled" /> 
    <item 
     android:state_enabled="false" 
     android:drawable="@drawable/shape_row_disabled" /> 

    <!-- the list items are enabled and being pressed --> 
    <item 
     android:state_focused="true" 
     android:state_pressed="true" 
     android:drawable="@drawable/shape_row_transition" /> 
    <item 
     android:state_focused="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/shape_row_transition" /> 

    <!-- the list items are enabled and being used --> 
    <item 
     android:state_focused="true" 
     android:drawable="@drawable/shape_row_selected" /> 

    <!-- the default item --> 
    <item 
     android:drawable="@drawable/shape_row" /> 
</selector> 

引用的可繪是形狀,圓角矩形,像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
    <gradient 
     android:startColor="#EF6100" 
     android:centerColor="#FF8E00" 
     android:endColor="#EF6100" 
     android:angle="270" /> 
    <corners 
     android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp" /> 
</shape> 

所以,最終的結果應與通過默認用@繪製/ shape_row背景項一個ListView,然後不同取決於狀態的彩色背景。當我啓動我的列表時,一切狀態都處於活動狀態,比如項目獲得焦點,被按下等等,但是當沒有選擇任何項目(即默認視圖狀態)時,項目本身具有透明背景。我希望國家名單的最後一條規則,

<item android:drawable="@drawable/shape_row" /> 

會捕獲該狀態,但由於某種原因,它不起作用。我已經移動了一些東西並嘗試了不同的設置,而沒有任何設置如果我編輯用於在ListView中定義列表項的row.xml文件,並嘗試添加指向@ drawable/shape_row的特定android:background,每行都會獲得正確的背景,但是按下後,焦點上,等等狀態無法處理(或至少,不能被視爲背景永遠不會改變)。

任何人都可以點我在這裏向正確的方向?我接近於把這個睡覺,但嘗試幾乎所有的東西后,只是無法讓ListView項目採取默認背景和通過android:listSelector響應實施狀態列表。

感謝,

保羅

編輯:

還只是嘗試添加機器人:itemBackground = 「@繪製/ shape_row」 到main.xml中ListView控件,並不出所料沒有運氣(該文檔狀態它應該用來指定菜單項目的背景,但認爲它值得拍攝列表項目)。

另一件事情嘗試了,爲每個選擇器和項目定義添加了android:visible =「true」。 StateListDrawable文檔似乎表明"Provides initial visibility state of the drawable; the default value is false",但添加此變化沒有改變。編輯2:所以,基於以下由Qberticus所做的研究,似乎每個視圖只能有一組列表選擇器,這確認了列表選擇器的行爲。我也可以確認設置android:drawSelectorOnTop確實移動了所選項目的選擇器IN FRONT,但是這當然掩蓋了項目本身,因此我只能看到選擇器本身(在我的例子中是一個彩色形狀)。

這裏是清單的樣子設置背景:

normal, unselected list with background set

由於設置的背景,在外觀上沒有任何變化時選定的項目。如果我改變了android:drawSelectorOnTop指令爲true,選擇一個項目,我得到:

selected, with drawSelectorOnTop set to true

最後,如果我設置列表項沒有背景圖片,選擇正常工作,但當然,有沒有背景圖片爲StateSelector的最後一條規則似乎並沒有遵守(它不應該作爲一個包羅萬象的?)對非選擇的項目意味着沒有好的自定義圓角形狀:

selected, no background

所以,我的問題依然存在;如果我沒有爲每個列表項目設置背景,我可以看到選擇器正在工作,但未選中項目時沒有背景。如果我爲這個項目設置了一個背景,當它沒有被選中時它們看起來很棒,但是背景遮擋了選擇器。使用工作選擇器是否無法使用唯一形狀的列表項?

感謝誰比誰的輕量化。

回答

9

想通了這一點,問題是,當我通過android:listSelector="@drawable/list_selector"正確設置爲ListView的國家列表中listSelector,我還需要通過android:background="@drawable/list_background"

設置列表項目到另一個狀態列表背景在list_background中。XML狀態列表,我有:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
android:visible="true"> 

    <!-- the list items are disabled --> 
<item 
     android:state_window_focused="false" 
     android:drawable="@drawable/shape_row_disabled" /> 

    <!-- the list items are enabled, in focus but not being touched/pressed (idle) --> 
    <item 
     android:state_window_focused="true" 
     android:state_pressed="false" 
     android:state_focused="false" 
     android:state_selected="false" 
     android:drawable="@drawable/shape_row" /> 

    <!-- the catch-all --> 
    <item 
     android:drawable="@android:color/transparent" /> 
</selector> 

找到答案在這裏:

Changing background color of ListView items on Android

在上面我的列表選擇的同時,它完美的作品。

2

listSelector僅用於指定View將在同一地點選擇項目View繪製。即,每個ListView只有listSelector的一個實例。您可以指定是否可以在頂部繪製或使用drawSelectorOnTop

如果你想讓你所有的View都使用狀態列表,那麼你應該指定在哪兒定義了兒童View

我使用AbsListView作爲參考的來源。具體地說AbsListView#setSelector(Drawable)AbsListView#positionSelectorAbsListView#drawSelector,和AbsListView#dispatchDraw

AbsListView#drawSelector

private void drawSelector(Canvas canvas) { 
     if (shouldShowSelector() && mSelectorRect != null && !mSelectorRect.isEmpty()) { 
      final Drawable selector = mSelector; 
      selector.setBounds(mSelectorRect); 
      selector.draw(canvas); 
     } 
} 

AbsListView#positionSelector

void positionSelector(View sel) { 
    final Rect selectorRect = mSelectorRect; 
    selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom()); 
    positionSelector(selectorRect.left, selectorRect.top, selectorRect.right, 
      selectorRect.bottom); 

    final boolean isChildViewEnabled = mIsChildViewEnabled; 
    if (sel.isEnabled() != isChildViewEnabled) { 
     mIsChildViewEnabled = !isChildViewEnabled; 
     refreshDrawableState(); 
    } 
} 

源表明僅存在一個mSelector創建/使用,並且它被定位在相同邊界矩形選定項目。

+0

感謝您的辛勤工作!當你提到listSelector只用於每個ListView時,這就是我在ListView XML中指定的。設置每個單獨的List項目(他們是TextViews)有一個背景意味着listSelector被隱藏......沒有背景意味着每個TextView的透明背景,直到選擇器啓動。我嘗試使用ListView的drawSelectorOnTop指令,因爲我可以在背景中看到選擇器,但它也在前景之上,也掩蓋了TextView的內容。 – 2011-01-19 18:24:09

+0

也許這只是不可能有一個自定義(在我的情況下,四捨五入)ListView項目和適當的選擇器行爲......也許我需要指定整個ListView的背景,說一種顏色,然後選擇器將正確顯示。希望有圓角的ListView項目和選擇器交互,但也許這是不可能的。 – 2011-01-19 18:26:05

0

您可以爲子佈局和selector_background分配默認背景,在這種情況下,如果未選擇項目,則默認背景不會遮擋選擇器。它爲我工作。