2014年9月23日 星期二

Android 開發(六十八) listview header animation

最近由於工作的需要寫了一個很簡單的listview header的 animation


這個animation看起來很簡單,實際上也真的是不難啦...不過真的花了我不少時間tune...

首先,我們要做的事情很簡單,就是在itemclick的時候,把header長出來,然後當header長出來之後,在重新click一次就會退回去,所以我們首先會先想到使用
translationY的這個 property.

ObjectAnimator animX = ObjectAnimator.ofFloat(listview, "translationY", 0f, -textview.getHeight());
ObjectAnimator animY = ObjectAnimator.ofFloat(textview, "translationY", 0f, -textview.getHeight());


利用上面的code我們就可以依照我們希望的方法將head (Textview)長出來/藏起來了

But.........
事情永遠不會是我們想的那麼簡單,當利用tranlationY將view往上移動之後就會發現,整個listview變短了,為了避免掉這個問題,所以我們必須在移動完成之後,將listview的height 往下延伸,那要怎麼做?

RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) listview.getLayoutParams();
p.bottomMargin = -textview.getHeight();
listview.setLayoutParams(p);
這樣就可以做出一個簡單的動畫了,最後附上原始版 gist 供大家參考,
ps. code寫的非常凌亂而且有很多地方還可以修改,只是為了展現功能而寫的.


沒有留言:

張貼留言