Demo是基于SwipeMenuListView的基础上实现的。先看效果图:
实现思路:
我觉得其实无论些什么模块,必须先有一个清晰的实现思路,这会事倍功半。
建议先了解SwipeMenuListView和属性动画的使用。
这里关键是使用属性动画实现删除后ITEM的向上或者向下移动。触发删除后,把上面或者下面可见的ITEM加上向上移动的动画,然后动画结束后刷新listview即可。效果有点小瑕疵,因为隐藏的View没法获取。所以最下面向上移的时候会空出一个ITEM,因为时间间隔设的有点短,瑕疵效果不太明显。哪位大神可以给点意见这个怎么解决?
核心代码:
swipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
// open
break;
case 1:
// delete
startDeleteAnimator(position);
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
private void startDeleteAnimator(final int position) {
View view = swipeMenuListView.getChildAt(position - swipeMenuListView.getFirstVisiblePosition());
ObjectAnimator.ofFloat(view, "alpha", 1, 0).setDuration(20).start();
if (swipeMenuListView.getFirstVisiblePosition() != 0 && swipeMenuListView.getLastVisiblePosition() == swipeMenuListView.getCount() - 1) {
//向下移
for (int i = position - 1 - swipeMenuListView.getFirstVisiblePosition(); i >= 0; i--) {
View itemView = swipeMenuListView.getChildAt(i);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(itemView, "translationY", view.getHeight()).setDuration(200);
objectAnimator.start();
if (i == 0) {
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
data.remove(position);
adapter.notifyDataSetChanged();
// 动画结束后,恢复ListView所有子View的属性
for (int i = 0; i < swipeMenuListView.getChildCount(); ++i) {
View v = swipeMenuListView.getChildAt(i);
v.setAlpha(1f);
v.setTranslationY(0);
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
} else {
//向上移
if (position == swipeMenuListView.getCount() - 1) {
data.remove(position);
adapter.notifyDataSetChanged();
}
for (int i = position + 1 - swipeMenuListView.getFirstVisiblePosition(); i < swipeMenuListView.getChildCount(); i++) {
View itemView = swipeMenuListView.getChildAt(i);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(itemView, "translationY", -view.getHeight()).setDuration(200);
objectAnimator.start();
if (i == swipeMenuListView.getChildCount() - 1) {
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
data.remove(position);
adapter.notifyDataSetChanged();
// 动画结束后,恢复ListView所有子View的属性
for (int i = 0; i < swipeMenuListView.getChildCount(); ++i) {
View v = swipeMenuListView.getChildAt(i);
v.setAlpha(1f);
v.setTranslationY(0);
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
}
}
越来越懒了。Demo下载地址