在其**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>
說實話,我不太瞭解你的代碼。我想更改特定項目的背景,而不是整個列表。如果我決定避免選擇器可繪製,那麼主要問題是獲取項視圖(特別是可以是ViewGroup),因爲getChildAt()不是100%的證明。你的代碼並不建議這樣做。 – cyanide
我的代碼更改所選項目的背景。您可以查看轉換可繪製 –
TransitionDrawable不會自動選擇所選項目,而您提供的代碼部分不涉及OnItemClick參數(至少我看不到任何)。你能否提供更詳細的代碼? – cyanide