fix defaultitemanimator & simpleitemanimator

This commit is contained in:
houzh 2024-02-17 16:41:36 +08:00
parent adc95e49e7
commit aec64db3f7
3 changed files with 24 additions and 23 deletions

View File

@ -48,10 +48,10 @@ public:
} }
long getItemId(int position)override {return position;} long getItemId(int position)override {return position;}
void onItemRangeChanged(int positionStart, int itemCount, Object* payload)override{ void onItemRangeChanged(int positionStart, int itemCount, Object* payload)override{
LOGD("positionStart=%d itemCount=%d",positionStart,itemCount); LOGV("positionStart=%d itemCount=%d",positionStart,itemCount);
} }
void onItemRangeInserted(int positionStart, int itemCount)override{ void onItemRangeInserted(int positionStart, int itemCount)override{
LOGD("positionStart=%d itemCount=%d",positionStart,itemCount); LOGV("positionStart=%d itemCount=%d",positionStart,itemCount);
} }
}; };
int main(int argc,const char*argv[]){ int main(int argc,const char*argv[]){
@ -65,9 +65,9 @@ int main(int argc,const char*argv[]){
rv->setAdapter(adapter); rv->setAdapter(adapter);
DividerItemDecoration* decoration = new DividerItemDecoration(&app, LinearLayout::VERTICAL); DividerItemDecoration* decoration = new DividerItemDecoration(&app, LinearLayout::VERTICAL);
auto anim=rv->getItemAnimator(); auto anim=rv->getItemAnimator();
anim->setRemoveDuration(1000); anim->setRemoveDuration(100);
anim->setAddDuration(1000); anim->setAddDuration(100);
rv->getLayoutManager()->requestSimpleAnimationsInNextLayout(); rv->getLayoutManager()->requestSimpleAnimationsInNextLayout();
for(int i=0;i<100;i++){ for(int i=0;i<100;i++){
adapter->add(std::string("string ")+std::to_string(i)); adapter->add(std::string("string ")+std::to_string(i));
@ -77,7 +77,5 @@ int main(int argc,const char*argv[]){
rv->addItemDecoration(decoration); rv->addItemDecoration(decoration);
w->addView(rv); w->addView(rv);
w->requestLayout(); w->requestLayout();
Runnable run([&](){adapter->add("1000");adapter->notifyItemInserted(100);});
w->postDelayed(run,4000);
app.exec(); app.exec();
} }

View File

@ -128,7 +128,8 @@ void DefaultItemAnimator::animateRemoveImpl(RecyclerView::ViewHolder& holder) {
animation.setListener({}); animation.setListener({});
holder.itemView->setAlpha(1); holder.itemView->setAlpha(1);
dispatchRemoveFinished(holder); dispatchRemoveFinished(holder);
//mRemoveAnimations.remove(holder); auto it = std::find(mRemoveAnimations.begin(),mRemoveAnimations.end(),&holder);
mRemoveAnimations.erase(it);//mRemoveAnimations.remove(holder);
dispatchFinishedWhenDone(); dispatchFinishedWhenDone();
}; };
animation.setDuration(getRemoveDuration()).alpha(0).setListener(al).start(); animation.setDuration(getRemoveDuration()).alpha(0).setListener(al).start();
@ -146,16 +147,17 @@ void DefaultItemAnimator::animateAddImpl(RecyclerView::ViewHolder& holder) {
ViewPropertyAnimator& animation = view->animate(); ViewPropertyAnimator& animation = view->animate();
mAddAnimations.push_back(&holder);//add(holder); mAddAnimations.push_back(&holder);//add(holder);
Animator::AnimatorListener al; Animator::AnimatorListener al;
al.onAnimationStart=[this,&holder](Animator&animator,bool isReverse){ al.onAnimationStart = [this,&holder](Animator&animator,bool isReverse){
dispatchAddStarting(holder); dispatchAddStarting(holder);
}; };
al.onAnimationCancel=[&holder](Animator&){ al.onAnimationCancel=[&holder](Animator&){
holder.itemView->setAlpha(1); holder.itemView->setAlpha(1);
}; };
al.onAnimationEnd=[this,&holder,&animation](Animator& animator,bool isReverse){ al.onAnimationEnd = [this,&holder,&animation](Animator& animator,bool isReverse){
animation.setListener({}); animation.setListener({});
dispatchAddFinished(holder); dispatchAddFinished(holder);
//mAddAnimations.remove(holder); auto it = std::find(mAddAnimations.begin(), mAddAnimations.end(),&holder);
mAddAnimations.erase(it);//mAddAnimations.remove(holder);
dispatchFinishedWhenDone(); dispatchFinishedWhenDone();
}; };
animation.alpha(1).setDuration(getAddDuration()).setListener(al).start(); animation.alpha(1).setDuration(getAddDuration()).setListener(al).start();
@ -199,10 +201,10 @@ void DefaultItemAnimator::animateMoveImpl(RecyclerView::ViewHolder& holder, int
mMoveAnimations.push_back(&holder);//add(holder); mMoveAnimations.push_back(&holder);//add(holder);
Animator::AnimatorListener al; Animator::AnimatorListener al;
al.onAnimationStart=[this,&holder](Animator& animator,bool isReverse) { al.onAnimationStart = [this,&holder](Animator& animator,bool isReverse) {
dispatchMoveStarting(holder); dispatchMoveStarting(holder);
}; };
al.onAnimationCancel=[this,view,deltaX,deltaY](Animator& animator) { al.onAnimationCancel = [this,view,deltaX,deltaY](Animator& animator) {
if (deltaX != 0) { if (deltaX != 0) {
view->setTranslationX(0); view->setTranslationX(0);
} }
@ -210,10 +212,11 @@ void DefaultItemAnimator::animateMoveImpl(RecyclerView::ViewHolder& holder, int
view->setTranslationY(0); view->setTranslationY(0);
} }
}; };
al.onAnimationEnd=[this,&animation,&holder](Animator& animator,bool isReverse) { al.onAnimationEnd = [this,&animation,&holder](Animator& animator,bool isReverse) {
animation.setListener({}); animation.setListener({});
dispatchMoveFinished(holder); dispatchMoveFinished(holder);
//mMoveAnimations.remove(holder); auto it =std::find(mMoveAnimations.begin(),mMoveAnimations.end(),&holder);
mMoveAnimations.erase(it);//mMoveAnimations.remove(holder);
dispatchFinishedWhenDone(); dispatchFinishedWhenDone();
}; };
animation.setDuration(getMoveDuration()).setListener(al).start(); animation.setDuration(getMoveDuration()).setListener(al).start();
@ -236,7 +239,7 @@ bool DefaultItemAnimator::animateChange(RecyclerView::ViewHolder& oldHolder, Rec
oldHolder.itemView->setTranslationX(prevTranslationX); oldHolder.itemView->setTranslationX(prevTranslationX);
oldHolder.itemView->setTranslationY(prevTranslationY); oldHolder.itemView->setTranslationY(prevTranslationY);
oldHolder.itemView->setAlpha(prevAlpha); oldHolder.itemView->setAlpha(prevAlpha);
if (1/*newHolder != nullptr*/) { if (&newHolder != nullptr) {
// carry over translation values // carry over translation values
resetAnimation(newHolder); resetAnimation(newHolder);
newHolder.itemView->setTranslationX(-deltaX); newHolder.itemView->setTranslationX(-deltaX);
@ -259,16 +262,16 @@ void DefaultItemAnimator::animateChangeImpl(ChangeInfo& changeInfo) {
oldViewAnim.translationY(changeInfo.toY - changeInfo.fromY); oldViewAnim.translationY(changeInfo.toY - changeInfo.fromY);
Animator::AnimatorListener al; Animator::AnimatorListener al;
al.onAnimationStart=[this,&changeInfo](Animator& animator,bool isReverse) { al.onAnimationStart = [this,&changeInfo](Animator& animator,bool isReverse) {
dispatchChangeStarting(*changeInfo.oldHolder, true); dispatchChangeStarting(*changeInfo.oldHolder, true);
}; };
al.onAnimationEnd=[this,view,&changeInfo,&oldViewAnim](Animator& animator,bool isReverse) { al.onAnimationEnd = [this,view,&changeInfo,&oldViewAnim](Animator& animator,bool isReverse) {
oldViewAnim.setListener({}); oldViewAnim.setListener({});
view->setAlpha(1); view->setAlpha(1);
view->setTranslationX(0); view->setTranslationX(0);
view->setTranslationY(0); view->setTranslationY(0);
dispatchChangeFinished(*changeInfo.oldHolder, true); dispatchChangeFinished(*changeInfo.oldHolder, true);
auto it =std::find(mChangeAnimations.begin(),mChangeAnimations.end(),changeInfo.oldHolder); auto it = std::find(mChangeAnimations.begin(),mChangeAnimations.end(),changeInfo.oldHolder);
mChangeAnimations.erase(it);//remove(*changeInfo.oldHolder); mChangeAnimations.erase(it);//remove(*changeInfo.oldHolder);
dispatchFinishedWhenDone(); dispatchFinishedWhenDone();
}; };
@ -278,16 +281,16 @@ void DefaultItemAnimator::animateChangeImpl(ChangeInfo& changeInfo) {
ViewPropertyAnimator& newViewAnimation = newView->animate(); ViewPropertyAnimator& newViewAnimation = newView->animate();
mChangeAnimations.push_back(changeInfo.newHolder); mChangeAnimations.push_back(changeInfo.newHolder);
Animator::AnimatorListener al; Animator::AnimatorListener al;
al.onAnimationStart=[this,&changeInfo](Animator& animator,bool isReverse) { al.onAnimationStart = [this,&changeInfo](Animator& animator,bool isReverse) {
dispatchChangeStarting(*changeInfo.newHolder, false); dispatchChangeStarting(*changeInfo.newHolder, false);
}; };
al.onAnimationEnd=[this,newView,&newViewAnimation,&changeInfo](Animator& animator,bool isReverse) { al.onAnimationEnd = [this,newView,&newViewAnimation,&changeInfo](Animator& animator,bool isReverse) {
newViewAnimation.setListener({}); newViewAnimation.setListener({});
newView->setAlpha(1); newView->setAlpha(1);
newView->setTranslationX(0); newView->setTranslationX(0);
newView->setTranslationY(0); newView->setTranslationY(0);
dispatchChangeFinished(*changeInfo.newHolder, false); dispatchChangeFinished(*changeInfo.newHolder, false);
auto it =std::find(mChangeAnimations.begin(),mChangeAnimations.end(),changeInfo.newHolder); auto it = std::find(mChangeAnimations.begin(),mChangeAnimations.end(),changeInfo.newHolder);
mChangeAnimations.erase(it);//remove(changeInfo.newHolder); mChangeAnimations.erase(it);//remove(changeInfo.newHolder);
dispatchFinishedWhenDone(); dispatchFinishedWhenDone();
}; };

View File

@ -40,7 +40,7 @@ bool SimpleItemAnimator::animateAppearance(RecyclerView::ViewHolder& viewHolder,
return animateMove(viewHolder, preLayoutInfo->left, preLayoutInfo->top, return animateMove(viewHolder, preLayoutInfo->left, preLayoutInfo->top,
postLayoutInfo.left, postLayoutInfo.top); postLayoutInfo.left, postLayoutInfo.top);
} else { } else {
LOGD_IF(_DEBUG,"ADDED: %p with view %p",viewHolder,viewHolder); LOGD_IF(_DEBUG,"ADDED: %p with view %p",&viewHolder,&viewHolder);
return animateAdd(viewHolder); return animateAdd(viewHolder);
} }
} }