[英]withEndAction() triggering onEndAnimation() function?
這是我的代碼(簡體)
public class StockAdapter extends ArrayAdapter<Stock> implements Animator.AnimatorListener
{
//Global variable - StockAdapter adapter;
//Global varaiable - View animatingView;
View.OnTouchListener stockRowOnTouch=new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (action) {
case MotionEvent.ACTION_DOWN:
//code to get the cordintated\s
case MotionEvent.ACTION_UP:
// get the coodinates
if(row is swiped) //determined by the coordinates
{
animatingView=view;
view.animate().translationX(itemWidth).alpha(1).setDuration(500).setListener(adapter);
}
}
@Override
public void onAnimationEnd(Animator animator) {
animatingView.setTranslationX(0);
animatingView.animate().alpha(0.4f).setDuration(4000).withEndAction(new Runnable() {
@Override
public void run() {
//After the animation duration delete the stock
}
});
}
}
我要做什么
然后,另一個動畫運行4秒鍾,其中視圖必須在該持續時間內消失。 4秒后,我將從列表視圖中刪除該行,並調用notifydatasetchanged()
。
我怎么做
對於第一個動畫(在500毫秒內平移窗口中的行),我設置了一個偵聽器StockAdapter
類對象本身。
onAnimationEnd()
函數中,我已將視圖轉換回其原始位置,然后運行淡入淡出動畫,但這一次使用了withEndAction
。 怎么了
onAnimationEnd()
函數,並且可以看到對該視圖所做的更改。 withEndAction()
),並在4秒鍾后從列表視圖中刪除該行並對其進行刷新。
在驗證日志時, withEndAction()
似乎正在觸發onEndAnimation()
。 這是某種錯誤嗎?
注意
我簽出了View , ViewPropertyAnimator和ViewRootImpl的源代碼。 不建議withEndAction再次調用相同的偵聽器。 如果我錯了,有人可以指出這一點嗎? 的函數調用停止在postDelayed()
的方法RunQueue
類的ViewRootImpl
。
您需要在onAnimationEnd()
重置動畫偵聽器,以避免觸發與您先前在onTouchListener()
設置的偵聽器相同的偵聽器。 從官方文檔尚不清楚,但是View.animate()
將嘗試為您的視圖重用同一ViewPropertyAnimator
,包括您先前設置的偵聽器。
下面的代碼應該可以解決問題:
...
animatingView.animate().alpha(0.4f).setDuration(4000).setListener(null).withEndAction(...)
...
問題:
.setListener(adapter)
由於您在4秒鍾后將Animator.AnimatorListener
應用於animatingView's ViewPropertyAnimator
,它將執行您的withEndAction
但在記住您將其設置為animatingView
的偵聽器之后 ,出現了問題,由於您使用的是與相同的引用 ,因此將一次又一次執行onAnimationEnd
animatingView
的ViewPropertyAnimator
。
解:
刪除該行后,您需要取消withEndAction
內部的動畫,以防止該動畫再次調用接口方法 。
樣品:
@Override
public void onAnimationEnd(Animator animator) {
animatingView.setTranslationX(0);
animatingView.animate().alpha(0.4f).setDuration(4000).withEndAction(new Runnable() {
@Override
public void run() {
//After the animation duration delete the stock
animatingView.animate().cancel(); //will cancel the animation and listener
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.