2011-12-19 98 views
9

我知道android不會在TouchMode中突出顯示任何內容。但我正在做一些類似於gmail應用程序的東西,在這個應用程序中,您從左側選擇了一些東西,並在活動的右側顯示詳細信息(想知道Google如何做到這一點)。如何突出顯示ListView中的選定項目?

所以故事是我必須突出顯示在左側ListView中選擇了什麼。我已經發現了一些類似的問題和解決方案基本上是:

1.override適配器的getView方法的setBackground的觀點onItemClick的選定位置

2.setBackground並清除花葯選擇

但是,由於一個奇怪的行爲,他們都沒有爲我工作:當我點擊一個項目並突出顯示它時,它後面的第五個項目也突出顯示,以此類推,當我向下滾動列表時。

有什麼建議嗎?謝謝!

回答

21

使用listView.setChoiceMode(int choiceMode);

參數

choiceMode一個CHOICE_MODE_NONE,CHOICE_MODE_SINGLE的,或從CHOICE_MODE_MULTIPLE類android.widget.AbsListView

http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode(int)

您還需要添加MultiChoiceModeListener,你可以有CHOICE_MODE_SINGLE

(android.widget.AbsListView.MultiChoiceModeListener)

參照下面

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List16.html

+0

是否可以做任何事情?我試過,但選擇器不留儘管.. – Han 2011-12-19 19:20:08

+0

添加更多解釋 – 2011-12-19 19:25:37

+0

非常感謝!通過添加MultiChoiceModeListener並將適配器的背景設置在它的xml文件中工作!但我想知道爲什麼Android只有在選擇項目時才採用背景顏色..所以自動 – Han 2011-12-19 22:09:47

1

您突出顯示多個項目的原因可能是因爲您要麼:重新使用整個視圖,要麼將這兩個視圖的背景設置爲相同的Drawable實例。如果屏幕上有兩次相同的drawable,則第一個發生的所有事件都會發生在所有其他事件上,因爲該邏輯是在Drawable本身的實例中實現的。

爲了解決這個問題,無論是:不要再使用了多行意見,或者不重複使用的多行可繪製(創建一個新的每次)

我知道這聽起來大量資源,而且它是的,但除非你有更好的解決方案,否則這是最簡單的解決方案。

+0

感謝的人..現在我想看看那裏的代碼重用的觀點,因爲我沒有寫的。順便說一句,是否有可能讓選擇器停留在頂部而不是改變背景?如果它是可行的,它會更有意義。 – Han 2011-12-19 19:31:31

0

樣品因爲在列表視圖模仿頂部的那些物品,他們還模仿他們的背景。您必須在getView()函數中設置項目的每個背景。在getView()中的每個項目中,您必須爲所選項目和未選項目設置背景。

16

這是定製listactivity或ListView中ListFragment

高亮選擇的項目

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) 
{ 

    for(int a = 0; a < parent.getChildCount(); a++) 
    { 
     parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT); 
    } 

    view.setBackgroundColor(Color.GREEN); 
} 
2

//// @繪製/ list_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:drawable="@drawable/list_item_bg_normal" 
android:state_pressed="false" android:state_selected="false" android:state_activated="false"/> 

<item android:drawable="@drawable/list_item_touch" 
android:state_pressed="true"/> 

<item android:drawable="@drawable/list_item_touch" 
android:state_pressed="false" android:state_selected="true"/> 

<item android:drawable="@drawable/list_item_bg_pressed" 
android:state_activated="true"/> 

</selector> 

//////// //////////// and on ListView

<ListView 
    android:id="@+id/list_slidermenu" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:choiceMode="singleChoice" 
    android:dividerHeight="1dp" 
    android:listSelector="@drawable/list_selector" 
    android:background="@color/list_background" 
    android:divider="#060606"/> 

/////////////////////// ListView項

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/list_selector"> 

    <ImageView 
    android:id="@+id/icon" 
    android:layout_width="35dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="12dp" 
    android:layout_marginRight="12dp" 
    android:contentDescription="@string/desc_list_item_icon" 
    android:src="@drawable/ic_home" 
    android:layout_centerVertical="true" /> 

    </RelativeLayout> 
0

以此爲列表選擇:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/list_bg" /> 
</selector> 

list_bg

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@color/list_bg_color" /> 
</shape> 

選擇自己偏愛的高亮顏色