2013-05-09 91 views
2

我想突出默認情況下我的列表視圖中的位置。Android的列表視圖選擇默認項目

下面是代碼: 我的列表視圖聲明:

<ListView 
android:id="@+id/list" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"/> 

項目列表聲明

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dip" 
    android:textSize="20sp" 
    android:textStyle="bold" /> 

</LinearLayout> 

在我的自定義選擇,我有:

<item 
    android:drawable="@android:color/holo_green_light" 
    android:state_selected="true"/> 

現在我想用默認的綠色標記在我的列表視圖中的一個位置。 Java代碼:

listview.setSelector(R.drawable.listselector); 
listview.setSelection(20); 

但所選元素不與綠顏色突出顯示。我嘗試了很多提出的解決方案,但沒有成功。 任何想法? 在此先感謝。

回答

1

嘗試在ListView XML設置選擇:

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:listSelector="@drawable/listselector" 
    /> 

此外,定義選擇item這樣的:

android:background="@android:color/holo_green_light" 

,而不是android:drawable

+0

它不會以這種方式工作..仍然沒有突出 – Steffi 2013-05-09 08:40:47

+1

請分享您的代碼的其餘部分。完整的選擇器XML和您從中設置選擇的類。你是否記得在將它添加到XML之後刪除'listview.setSelector(R.drawable.listselector);'? – tbkn23 2013-05-09 10:03:13

1

使用本

your_list_view.setItemChecked(0,TRUE); //「0」爲可選擇項目位置

爲我工作:)