2013-12-09 56 views
1

一個ListView聲明與android:choiceMode="singleChoice」。突出顯示與蜂窩前預選的ListView項目?

要突出當前選擇的項目,我宣佈選擇作爲背景繪製一個列表項(或listSelector爲ListView),其中包括線

<item android:state_activated="true" android:drawable="@color/chosen" />` 

它的偉大工程。 ..與Android 3.0+,因爲查看激活財產出現直到SDK 11

有沒有辦法來突出所選項目與Android 2?我想到一個簡單的和「自然」的解決方案(沒有搞鬼升ike listView.getChildAt(position).setBackgroundColor(...)),因爲choiceMode功能從一開始就存在,完全無法使用而沒有突出顯示。

通過提供相關答案,您將真正做到我的一天。

回答

0

在其**onItemClick**聽衆,你可以使用這樣的**TransitionDrawable**。我把這個代碼放在getView()和highlightPosition是項目的位置。你可以根據你的需要修改它,並且reImage是充氣機xml上的相對佈局。我已經把這段代碼放在getView中。您可以嘗試把它放在onItemClick..This代碼突出顯示所選擇的項目2 3秒鐘,然後顏色從藍色漸變爲白色: -

  if(highlightPosition == position){ 
       viewHolderThubnail.relImage.setBackgroundResource(R.drawable.translate); 
       TransitionDrawable transition = (TransitionDrawable) viewHolderThubnail.relImage.getBackground(); 
       transition.startTransition(1000); 
       highlightPosition =-1; 
      }else{ 
       viewHolderThubnail.relImage.setBackgroundResource(R.color.white); 
      } 

translate.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <transition xmlns:android="http://schemas.android.com/apk/res/android"> 
      <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. --> 
      <item android:drawable="@drawable/new_state" /> 
      <item android:drawable="@drawable/original_state" /> 
    </transition> 

new_state.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle"> 
    <solid android:color="#92BCE7"/> 
</shape> 

original_state.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle"> 
    <solid android:color="#FFFFFF"/> 
</shape> 
+0

說實話,我不太瞭解你的代碼。我想更改特定項目的背景,而不是整個列表。如果我決定避免選擇器可繪製,那麼主要問題是獲取項視圖(特別是可以是ViewGroup),因爲getChildAt()不是100%的證明。你的代碼並不建議這樣做。 – cyanide

+0

我的代碼更改所選項目的背景。您可以查看轉換可繪製 –

+0

TransitionDrawable不會自動選擇所選項目,而您提供的代碼部分不涉及OnItemClick參數(至少我看不到任何)。你能否提供更詳細的代碼? – cyanide