Listview Item进入和删除动画

从右边划入的动画

  • 定义xml动画
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@integer/animTime"
android:fromXDelta="100%p"
android:toXDelta="0%p" >
</translate>
  • Adapter里只需要写如下代码
1
2
3
4
5
6
7
8
9
10
11
12
@Override
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
....
if (!junk.isAnimatedBefore()) {
junk.setAnimatedBefore(true);
convertView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_left_in));
}else{
convertView.clearAnimation();
}
return convertView;
}

从左边划出的动画

  • 定义xml动画
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true"
android:duration="@integer/animTime">
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p">

</translate>
</set>
  • Adapter里不需要做什么,需要在外面操作ListView.

在外面操作ListView

  • 开始动画
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
private void startBoost() {
mIsDeleting = true;
boolean hasAnimation = false;
long offset = 0;
mAnimCount = 0;
int groupPosition = -1;
int childPosition = -1;
Iterator<ArrayList<Junk>> listIterator = mChildren.iterator();
while (listIterator.hasNext()) {
groupPosition++;
final Iterator<Junk> junkIterator = listIterator.next().iterator();
while (junkIterator.hasNext()) {
childPosition++;
Junk junk = junkIterator.next();
if (junk.isChecked()) {
...
junkIterator.remove();
final View itemView = ListViewUtil.getChildItemView(this,
mListView, groupPosition, childPosition);
if (itemView != null) {
hasAnimation = true;
((Animation) itemView.getTag(R.id.anim)).setAnimationListener(mAnimationListener);
((Animation) itemView.getTag(R.id.anim)).setStartOffset(offset);
offset += 100;
itemView.startAnimation(((Animation) itemView.getTag(R.id.anim)));
}
}
}
}
if (!hasAnimation) {
deleteCompleted();
}
}
  • mAnimationListener定义如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private Animation.AnimationListener mAnimationListener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mAnimCount++;
}

@Override
public void onAnimationEnd(Animation animation) {
mAnimCount--;
if (mAnimCount == 0) {
deleteCompleted();
}
}

@Override
public void onAnimationRepeat(Animation animation) {

}
};
  • 删除完成后需要调用ListViewUtil.clearListViewAnim(this, mListView);,否则原来动画的地方会显示空白。
  • 动画时要禁用OnTouch事件,因为我们做动画时并没有调用mAdapter.notifyDataSetChanged(),所以如果滚动,由于数据没有同步,肯定会挂掉。

禁用OnTouch事件的方法如下

1
2
3
4
5
6
7
8
9
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// intercept touch event when deleting ,avoid listview get view.
if (mIsDeleting) {
return true;
}

return super.dispatchTouchEvent(ev);
}

ListView的工具方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public static View getChildItemView(Context context, FloatingGroupExpandableListView listView, int groupPosition, int childPosition) {
long packedPosition = ExpandableListView.getPackedPositionForChild(groupPosition, childPosition);
int flatPosition;
try {
flatPosition = listView.getFlatListPosition(packedPosition);
} catch (Exception e) {
e.printStackTrace();
return null;
}
int firstPosition = listView.getFirstVisiblePosition();
int wantedChild = flatPosition - firstPosition;
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
return null;
}
View childItemView = listView.getChildAt(wantedChild);
childItemView.setTag(R.id.anim, AnimationUtils.loadAnimation(context, R.anim.slide_left_out));
return childItemView;
}

public static View getItemView(Context context, ListView listView, int position) {
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount();
int wantedChild = position - firstPosition;
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
return null;
}
View wantedView = listView.getChildAt(wantedChild);
wantedView.setTag(R.id.anim, AnimationUtils.loadAnimation(context, R.anim.slide_left_out));
return wantedView;
}

public static void clearListViewAnim(Context context, ListView listView) {
int first = listView.getFirstVisiblePosition();
int count = listView.getChildCount() + 2;
for (int i = first; i < first + count; i++) {
View itemView = getItemView(context, listView, i);
if (itemView != null) {
itemView.clearAnimation();
}
}
}



The link of this page is https://blog.nooa.tech/articles/fb92c8a1/ . Welcome to reproduce it!

© 2018.02.08 - 2024.05.25 Mengmeng Kuang  保留所有权利!

:D 获取中...

Creative Commons License