-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathActivityAnimation16.java
141 lines (116 loc) · 4.26 KB
/
ActivityAnimation16.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.xibei.personaldesign.itemDemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.xibei.personaldesign.R;
import java.util.ArrayList;
import java.util.List;
public class ActivityAnimation16 extends Activity {
ListView lv_data;
List<String> datas;
ListAdapter adapter;
private int mFirstTop, mFirstPosition;
private boolean isScrollDown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_animation_16);
lv_data = findViewById(R.id.lv_data);
initData();
adapter = new ListAdapter();
lv_data.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
View firstChild = view.getChildAt(0);
if (firstChild == null) return;
int top = firstChild.getTop();
/**
* firstVisibleItem > mFirstPosition表示向下滑动一整个Item
* mFirstTop > top表示在当前这个item中滑动
*/
isScrollDown = firstVisibleItem > mFirstPosition || mFirstTop > top;
mFirstTop = top;
mFirstPosition = firstVisibleItem;
}
});
lv_data.setAdapter(adapter);
}
private void initData() {
datas = new ArrayList<>();
for (int i = 0; i < 30; i++) {
datas.add("第" + i + "个item");
}
}
public int getbgColor(int position) {
if (position % 4 == 0) {
return Color.argb(255, 220, 20, 60);
} else if (position % 4 == 1) {
return Color.argb(255, 255, 165, 0);
} else if (position % 4 == 2) {
return Color.argb(255, 255, 255, 0);
} else if (position % 4 == 3) {
return Color.argb(255, 0, 165, 0);
}
return 0;
}
class ListAdapter extends BaseAdapter {
private Animation animation;
public ListAdapter() {
animation = AnimationUtils.loadAnimation(ActivityAnimation16.this, R.anim.bottom_in_anim);
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
convertView = LayoutInflater.from(ActivityAnimation16.this).inflate(R.layout.item_list16, null);
holder = new Holder();
holder.ll_backround = convertView.findViewById(R.id.ll_background);
holder.tv_number = convertView.findViewById(R.id.tv_number);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
//清除当前显示区域中所有item的动画
for (int i=0;i<lv_data.getChildCount();i++){
View view = lv_data.getChildAt(i);
view.clearAnimation();
}
//然后给当前item添加上动画
if (isScrollDown) {
convertView.startAnimation(animation);
}
holder.tv_number.setText(datas.get(position));
holder.ll_backround.setBackgroundColor(getbgColor(position));
return convertView;
}
}
class Holder {
LinearLayout ll_backround;
TextView tv_number;
}
}