2012-05-22 14 views
0

我試圖找到某種優雅的解決方案,用於淡入/淡出屬於ListView中項目一部​​分的TextView。在一個ListView項中淡入一個視圖 - 有一個轉折

爲了給你一些上下文,列表視圖顯示了一個籃球比賽中的球員列表。用戶點擊一個名字,並提供一個對話來記錄一個事件,例如該球員的投籃或犯規。一旦對話被解除,用戶就會回到列表視圖,這裏我想提供一些關於剛剛記錄的事件的反饋。

我想這樣做的方式是在剛剛點擊的項目(播放器)的視圖中出現約5秒鐘的小字符串。小字符串會顯示「第3次犯規」或「4次失誤」。

一個簡單的實現很簡單。將視圖的文本更改爲所需的字符串,然後開始淡入視圖的動畫,將其保留一段時間,然後淡出。然而,當第一次同一玩家的第二次事件被記錄時,會出現問題。理想情況下,應該允許第一個反饋字符串保持分配的5秒,第二個字符串應該在接下來的5秒內淡入/淡出。

這個排隊的動畫和文字變化在每個玩家基礎我不太確定如何實現。此外,我擔心動畫與活動的生命週期之間的相互作用。當活動發送到後臺,停止甚至從內存中移除時,發生(或應該發生)排隊的動畫會發生什麼?或者當一個項目從列表視圖後面的ArrayAdapter中移除時?

想法?

Manu

回答

1

不要擔心活動的生命週期。不會有不利影響。但是,如果活動在動畫過程中進入背景,則動畫將發生並且您不會看到它。

對於一個巡航能力動畫等下,只要做到這一點:

// here we will keep track of any listView and when the last animation took place. 
// The keys will be your listView identifiers. Here I assumed an integer, but use whatever is a good ID for your listView 
private HashMap<Integer, Long> listViewLastAnimations; 

// the length of the animation in milliseconds 
private static long ANIMATION_LENGTH_MS = 5000; 

// put this code where you would start your animation 
// get when the last event animation occurred 
Long lastAnimation = listViewLastAnimations.get(YOUR_LIST_ITEM_IDENTIFIER); 
Date new = new Date(); 
if (lastAnimation == null || 
    new.currentTimeMillis() - lastAnimation > ANIMATION_LENGTH_MS){ 
listViewLastAnimations.put(YOUR_LIST_ITEM_IDENTIFIER, new.currentTimeMillis()); 
// perform animation as normal 
}else{ 
// set a delay to your animation with 
long delay = ANIMATION_LENGTH_MS - (new.currentTimeMillis() - lastAnimation); 
listViewLastAnimations.put(YOUR_LIST_ITEM_IDENTIFIER, new.currentTimeMillis() + delay); 
setStartOffset(delay) ; 
} 
+0

謝謝凱爾的回答!我喜歡它的簡單!我希望在未來幾天能夠實施。我會告訴你! – manu3d