2016-09-23 3 views
1

내 애플리케이션에서 선택 모드로 Recyclerview를 만들었습니다. 사용자가 선택 모드를 시작하도록 선택하면 notifyDataSetChanged();Android RecyclerView가 모든 항목에 애니메이션을 적용하지 않습니다.

및 fromBindViewHolder() 사용자가 항목을 확인/선택할 수 있도록 확인란을 표시하기 위해 레이아웃 항목을 약간 움직이는 방법을 호출합니다.

내 문제는 위 또는 아래로 스크롤 할 때 애니메이션이 효과에 영향을 미치지 않는다는 것입니다. 어떻게 처리 할 수 ​​있습니까? 여기

내 코드의 예입니다

어댑터 :

@Override 
    public void onBindViewHolder(BaseViewHolder holder, int position) { 

     if(startSelectionMode()){ 
      holder.openAnimation(); 
     }else{ 
      holder.closeAnimation(); 
     }  
    } 

ViewHolder :

public void openAnimation() { 
    containerView.animate().translationX(checkBox.getWidth()).setDuration(300).start(); 
} 

public void closeAnimation() { 
    containerView.animate().translationX(0).setDuration(300).start(); 

} 
+0

내가 당신에게 애니메이션 작품 일부 수정 된 코드를 제공하거나 수정해야 당신의 AnimationUtils 클래스이 될 것이다 onBindViewHolder 방법

if (position > previousPosition) { AnimationUtils.animate(holder, true); } else { AnimationUtils.animate(holder, false); } previousPosition = position; 

을 지난에서이 작업을 수행 이 코드 –

+0

나는 작동하는 수정 된 코드를 얻게되어 기쁘고 스크롤 할 때 등 작업 애니메이션의 예제를 볼 수 있습니다. – Kick

+0

내 대답을 확인하고 알려주십시오. 도움이된다. –

답변

0

당신은 같은 것을 할 수 있습니다. 어댑터 클래스에 아래 코드를 추가하고 StudentBean을 사용하여 Object 클래스 이름을 수정하십시오.

는 전역 변수

private Integer previousPosition = 0; 

에서 어댑터 클래스에 추가합니다.

public StudentBean removeItem(int position) { 
    final StudentBean model = mModels.remove(position); 
    notifyItemRemoved(position); 
    return model; 
} 

public void addItem(int position, StudentBean model) { 
    mModels.add(position, model); 
    notifyItemInserted(position); 
} 

public void moveItem(int fromPosition, int toPosition) { 
    final StudentBean model = mModels.remove(fromPosition); 
    mModels.add(toPosition, model); 
    notifyItemMoved(fromPosition, toPosition); 
} 

public void animateTo(List<StudentBean> models) { 
    applyAndAnimateRemovals(models); 
    applyAndAnimateAdditions(models); 
    applyAndAnimateMovedItems(models); 
} 

private void applyAndAnimateRemovals(List<StudentBean> newModels) { 
    for (int i = mModels.size() - 1; i >= 0; i--) { 
     final StudentBean model = mModels.get(i); 
     if (!newModels.contains(model)) { 
      removeItem(i); 
     } 
    } 
} 

private void applyAndAnimateAdditions(List<StudentBean> newModels) { 
    for (int i = 0, count = newModels.size(); i < count; i++) { 
     final StudentBean model = newModels.get(i); 
     if (!mModels.contains(model)) { 
      addItem(i, model); 
     } 
    } 
} 

private void applyAndAnimateMovedItems(List<StudentBean> newModels) { 
    for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) { 
     final StudentBean model = newModels.get(toPosition); 
     final int fromPosition = mModels.indexOf(model); 
     if (fromPosition >= 0 && fromPosition != toPosition) { 
      moveItem(fromPosition, toPosition); 
     } 
    } 
} 

public class AnimationUtils { 
    private static int counter = 0; 

    public static void scaleXY(RecyclerView.ViewHolder holder) { 
     holder.itemView.setScaleX(0); 
     holder.itemView.setScaleY(0); 

     PropertyValuesHolder propx = PropertyValuesHolder.ofFloat("scaleX", 1); 
     PropertyValuesHolder propy = PropertyValuesHolder.ofFloat("scaleY", 1); 

     ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(holder.itemView, propx, propy); 


     animator.setDuration(800); 
     animator.start(); 
    } 

    public static void scaleX(RecyclerView.ViewHolder holder) { 
     holder.itemView.setScaleX(0); 

     PropertyValuesHolder propx = PropertyValuesHolder.ofFloat("scaleX", 1); 

     ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(holder.itemView, propx); 


     animator.setDuration(800); 
     animator.start(); 
    } 

    public static void scaleY(RecyclerView.ViewHolder holder) { 
     holder.itemView.setScaleY(0); 

     PropertyValuesHolder propy = PropertyValuesHolder.ofFloat("scaleY", 1); 

     ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(holder.itemView, propy); 

     animator.setDuration(800); 
     animator.start(); 
    } 

    public static void animate(RecyclerView.ViewHolder holder, boolean goesDown) { 

     /* YoYo.with(Techniques.RubberBand) 
       .duration(1000) 
       .playOn(holder.itemView);*/ 
//  AnimatorSet animatorSet = new AnimatorSet(); 
//  ObjectAnimator animatorScaleX = ObjectAnimator.ofFloat(holder.itemView, "scaleX" ,0.5F, 0.8F, 1.0F); 
//  ObjectAnimator animatorScaleY = ObjectAnimator.ofFloat(holder.itemView, "scaleY", 0.5F, 0.8F, 1.0F); 
     ObjectAnimator animatorTranslateY = ObjectAnimator.ofFloat(holder.itemView, "translationY", goesDown == true ? 300 : -300, 0); 
//  ObjectAnimator animatorTranslateX = ObjectAnimator.ofFloat(holder.itemView, "translationX", -50, 50, -30, 30, -20, 20, -5, 5, 0); 
//  animatorSet.playTogether(animatorTranslateX, animatorTranslateY, animatorScaleX, animatorScaleY); 
//  animatorSet.setInterpolator(new AnticipateInterpolator()); 
//  animatorSet.setDuration(1000); 
//  animatorSet.start(); 
     animatorTranslateY.setDuration(1000); 
     animatorTranslateY.start(); 

    } 

    public static void animateToolbarDroppingDown(View containerToolbar) { 

     containerToolbar.setRotationX(-90); 
     containerToolbar.setAlpha(0.2F); 
     containerToolbar.setPivotX(0.0F); 
     containerToolbar.setPivotY(0.0F); 
     Animator alpha = ObjectAnimator.ofFloat(containerToolbar, "alpha", 0.2F, 0.4F, 0.6F, 0.8F, 1.0F).setDuration(4000); 
     Animator rotationX = ObjectAnimator.ofFloat(containerToolbar, "rotationX", -90, 60, -45, 45, -10, 30, 0, 20, 0, 5, 0).setDuration(8000); 
     AnimatorSet animatorSet = new AnimatorSet(); 
     animatorSet.setInterpolator(new DecelerateInterpolator()); 
     animatorSet.playTogether(alpha, rotationX); 
     animatorSet.start(); 
    } 

    /** 
    * Courtesy: Vladimir Topalovic 
    * 
    * @param holder 
    * @param goesDown 
    */ 
    public static void animate1(RecyclerView.ViewHolder holder, boolean goesDown) { 
     int holderHeight = holder.itemView.getHeight(); 
     holder.itemView.setPivotY(goesDown == true ? 0 : holderHeight); 
     AnimatorSet animatorSet = new AnimatorSet(); 
     ObjectAnimator animatorTranslateY = ObjectAnimator.ofFloat(holder.itemView, "translationY", goesDown == true ? 300 : -300, 0); 
     ObjectAnimator scaleY = ObjectAnimator.ofFloat(holder.itemView, "scaleY", 1f, 0.4f, 1f); 
     ObjectAnimator scaleX = ObjectAnimator.ofFloat(holder.itemView, "scaleX", 1f, 1.3f, 1f); 
     animatorTranslateY.setInterpolator(new AccelerateInterpolator()); 
     scaleY.setInterpolator(new OvershootInterpolator()); 
     scaleX.setInterpolator(new OvershootInterpolator()); 
     animatorSet.play(animatorTranslateY).before(scaleY).before(scaleX); 
     animatorSet.setDuration(700); 
     animatorSet.start(); 
    } 

    /** 
    * Courtesy: Vladimir Topalovic 
    * 
    * @param holder 
    * @param goesDown 
    */ 
    public static void animateSunblind(RecyclerView.ViewHolder holder, boolean goesDown) { 
     int holderHeight = holder.itemView.getHeight(); 
     holder.itemView.setPivotY(goesDown == true ? 0 : holderHeight); 
     holder.itemView.setPivotX(holder.itemView.getHeight()); 
     AnimatorSet animatorSet = new AnimatorSet(); 
     ObjectAnimator animatorTranslateY = ObjectAnimator.ofFloat(holder.itemView, "translationY", goesDown == true ? 300 : -300, 0); 
     ObjectAnimator animatorRotation = ObjectAnimator.ofFloat(holder.itemView, "rotationX", goesDown == true ? -90f : 90, 0f); 
     ObjectAnimator animatorScaleX = ObjectAnimator.ofFloat(holder.itemView, "scaleX", 0.5f, 1f); 
     animatorSet.playTogether(animatorTranslateY, animatorRotation, animatorScaleX); 
     animatorSet.setInterpolator(new DecelerateInterpolator(1.1f)); 
     animatorSet.setDuration(1000); 
     animatorSet.start(); 
    } 

    /** 
    * Courtesy: Vladimir Topalovic 
    * 
    * @param holder 
    * @param goesDown 
    */ 
    public static void animateScatter(RecyclerView.ViewHolder holder, boolean goesDown) { 
     counter = ++counter % 4; 
     int holderHeight = holder.itemView.getHeight(); 
     int holderWidth = holder.itemView.getWidth(); 
     View holderItemView = holder.itemView; 
     holderItemView.setPivotY(goesDown == true ? 0 : holderHeight); 
     holderItemView.setPivotX(holderWidth/2); 
     AnimatorSet animatorSet = new AnimatorSet(); 
     ObjectAnimator animatorTranslateY = ObjectAnimator.ofFloat(holderItemView, "translationY", goesDown == true ? 300 : -300, 0); 
     ObjectAnimator animatorTranslateX = ObjectAnimator.ofFloat(holderItemView, "translationX", counter == 1 || counter == 3 ? holderWidth : -holderWidth, 0); 
     ObjectAnimator animatorScaleX = ObjectAnimator.ofFloat(holderItemView, "scaleX", counter == 1 || counter == 2 ? 0 : 2, 1f); 
     ObjectAnimator animatorScaleY = ObjectAnimator.ofFloat(holderItemView, "scaleY", counter == 1 || counter == 2 ? 0 : 2, 1f); 
     ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(holderItemView, "alpha", 0f, 1f); 
     animatorAlpha.setInterpolator(new AccelerateInterpolator(1.5f)); 
     animatorSet.playTogether(animatorAlpha, animatorScaleX, animatorScaleY, animatorTranslateX, animatorTranslateY); 
     animatorSet.setDuration(2000).setInterpolator(new DecelerateInterpolator(1.1f)); 
     animatorSet.start(); 
    } 

//  ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(holder.itemView, "translationY", (positive == true ? 200.0F : -200.F), 0F); 
//  objectAnimator.setInterpolator(new DecelerateInterpolator()); 
//  objectAnimator.setDuration(1000); 
//  objectAnimator.start(); 
//  YoYo.with(Techniques.StandUp).duration(800).playOn(holder.itemView); 
//  AnimatorSet animatorSet = new AnimatorSet(); 
//  Animator scaleVertical = ObjectAnimator.ofFloat(holder.itemView,"scaleY",1.0F,0.8F,1.2F,1.4F,1.6F,1.4F,1.2F,0.8F,1.0F).setDuration(2000); 
//  Animator rotateY = ObjectAnimator.ofFloat(holder.itemView,"rotationY",0,5,10,15,20,25,30,25,20,15,10,5,0).setDuration(2000); 
//  //ObjectAnimator.ofFloat(holder.itemView,"scaleY",1.0F,0.8F,1.2F,1.4F,1.6F,1.4F,1.2F,0.8F,1.0F).setDuration(2000) 
//  //ObjectAnimator.ofFloat(holder.itemView,"rotationY",0,5,10,15,20,25,30,25,20,15,10,5,0).setDuration(2000); 
// 
//  animatorSet.playTogether(rotateY, scaleVertical); 
//  animatorSet.start(); 


// 
} 
관련 문제