2013-02-01 138 views
1

在我的android應用程序中,我有列表視圖和每個列表項的詳細視圖。對於平板電腦,我顯示了項目列表視圖和選定項目的詳細視圖,如下所示。如何在android中突出顯示選定的項目?

enter image description here

所以我的問題是我怎麼能高亮顯示所選項目用戶點擊列表項之後。

我使用一個BaseAdapter我view.How可以做到這一點任何想法來加載列表?

編輯:

是作爲chintan khetiya提到我用下面的XML文件的列表項的背景,但也不會高興選定的項目。我錯過了什麼?

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
android:state_selected="false" 
    android:state_pressed="false" 
    android:drawable="@color/abs__background_holo_light" /> 
<item android:state_pressed="true" 
    android:drawable="@color/action_bar_blue" /> 
<item android:state_selected="true" 
android:state_pressed="false" 
    android:drawable="@color/action_bar_blue" /> 
</selector> 

回答

2

您的疑問:

我的問題是我怎麼能高亮顯示所選項目用戶點擊列表項之後。

我想你問選擇。意思是如果列表行處於焦點狀態,那麼它應該與所有其他行看起來不同。按下或觸摸行時也是如此。

對於你必須在文件夾可繪製文件Selector.xml,只是把那個選擇文件在列表中排

該文件應該有不同的標籤一樣Focus-Click-Press和更改繪製對象按狀態。

更新:

只需更換你的圖標,並在保存文件夾可繪製。

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

    <!-- Pressed --> 
    <item android:drawable="@drawable/p_paly_press" android:state_pressed="true"/> 

    <!-- selected --> 
    <item android:drawable="@drawable/p_play" android:state_selected="true"/> 

    <!-- focused --> 
    <item android:drawable="@drawable/p_paly_press" android:state_focused="true"/> 

    <!-- default --> 
    <item android:drawable="@drawable/p_play"/> 

</selector> 
+0

是的chintan khetiya我編輯了這個問題,你能告訴我該代碼的錯誤部分。 –

+0

我有更新答案。接受,如果它對你有幫助 –

+0

它爲我工作相同的代碼,我有粘貼在這裏。其他問題 –

1

您可以在styles.xml定義

<style name="Theme.Base" parent="..."> 
     <item name="activatableItemBackground">@drawable/activatable_item_background</item> 
    </style> 

    <style name="ListItemContainerBase"> 
     <item name="android:background">?activatableItemBackground</item> 
    </style> 

在res /繪製定義activatable_item_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/item_pressed" android:state_pressed="true" /> 
    <item android:drawable="@drawable/item_focused" android:state_focused="true" /> 
    <item android:drawable="@drawable/item_focused" android:state_selected="true" /> 
    <item android:drawable="@drawable/item_activated" android:state_activated="true" /> 
    <item android:drawable="@drawable/item_checked" android:state_checked="true" /> 
    <item android:drawable="@android:color/transparent" /> 
</selector> 

item_pressed,item_focused .....在水庫圖像/繪製-XXX

定義您的視圖中的每個條目的佈局是這樣的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/ListItemContainerBase"> 
2

類似下面的下面的代碼中的代碼可用於高亮顯示所選項目的肯定,如果其他方面不這樣做,你需要:

class Adapter extends ArrayAdapter<String> { 

    private int selectedPos = -1; 
    Drawable selectedBackground; 

    public MainSelectAdapter(Context context, int textViewResourceId, 
      List<String> objects) { 
     super(context, textViewResourceId, objects); 
     selectedBackground = 
       context.getResources().getDrawable(R.color.selecteditembackground); 
    } 
    public void setSelectedPosition(int pos){ 
     selectedPos = pos; 
     notifyDataSetChanged(); 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = super.getView(position, convertView, parent); 
     if (selectedPos == position) { 
      v.setBackgroundDrawable(selectedBackground); 
     } else { 
      v.setBackgroundDrawable(null); 
     } 
     return v; 
    } 
} 

而且在主要活動

Adapter adapter = new Adapter(this, android.R.layout.simple_list_item_1, 
    myItemsToShow); 

    list = (ListView) findViewById(R.id.flows); 
    list.setItemsCanFocus(true); 
    list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    public void onItemClick(AdapterView<?> adapter, View view, 
       int pos, long id) { 
       adapter.setSelectedPosition(pos); 
    } 
    }); 

通過這種方法,您可以對選定的項目高亮顯示進行自己的控制,您可以通過偵聽器捕獲選擇事件,併爲自己設置所需的Drawable。

+0

這種方法適用於'ArrayAdapter',但如果某人使用'CursorAdapter',該怎麼辦? –

相關問題