在我的应用程序中,我使用选择模式制作了Recyclerview。 当用户选择启动选择模式时,我调用notifyDataSetChanged();
和onBindViewHolder() 我调用一种方法来设置布局项的动画,以便公开一个复选框,以便用户可以检查/选择项目。
我的问题是当我向上或向下滚动时有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 :(得分:0)
你可以这样做。在 adapter 类中添加以下代码,并使用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);
}
}
}
最后在你的onBindViewHolder方法
中执行此操作 if (position > previousPosition) {
AnimationUtils.animate(holder, true);
} else {
AnimationUtils.animate(holder, false);
}
previousPosition = position;
这将是您的AnimationUtils类
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();
//
}