2016-09-23 20 views
1

私のアプリケーションでは、選択モードでRecyclerviewを作成しました。 ユーザが選択モードを開始することを選択すると、私はnotifyDataSetChanged()を呼び出します。Android RecyclerViewはすべてのアイテムをアニメーション化しません

とfromBindViewHolder() 私はレイアウトアイテムを少し動かしてチェックボックスを公開し、アイテムをチェック/選択できるメソッドを呼び出します。

私の問題は、上下にスクロールすると、 アニメーションがそれらに影響しない1〜3のアイテムです。 どうすれば対応できますか?ここ

は、私のコードの例です:

アダプタ:

@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(); 


// 
} 
関連する問題