Android 仿新浪微博的ListView下拉更新功能Word文件下载.docx
《Android 仿新浪微博的ListView下拉更新功能Word文件下载.docx》由会员分享,可在线阅读,更多相关《Android 仿新浪微博的ListView下拉更新功能Word文件下载.docx(42页珍藏版)》请在冰点文库上搜索。
16
id="
@+id/head_contentLayout"
17
paddingLeft="
30dp"
18
19
20
--箭头图像、进度条-->
21
FrameLayout
22
23
24
layout_alignParentLeft="
true"
25
layout_centerVertical="
26
27
28
--箭头-->
29
ImageView
30
31
32
layout_gravity="
center"
33
src="
@drawable/arrow_down"
34
@+id/head_arrowImageView"
35
/>
36
37
--进度条-->
38
ProgressBar
39
40
41
style="
attr/progressBarStyleSmall"
42
43
@+id/head_progressBar"
44
45
visibility="
gone"
46
47
48
/FrameLayout>
49
50
--提示、最近更新-->
51
52
53
54
layout_centerHorizontal="
55
orientation="
vertical"
56
gravity="
center_horizontal"
57
58
59
--提示-->
60
TextView
61
62
63
text="
下拉刷新"
64
textSize="
15dp"
65
@+id/head_tipsTextView"
66
67
68
--最近更新-->
69
70
71
72
@+id/head_lastUpdatedTextView"
73
上次更新"
74
12dp"
75
76
77
/LinearLayout>
78
79
80
/RelativeLayout>
81
82
83
然后设置item的布局模板item.xml:
LinearLayoutxmlns:
match_parent"
center_vertical"
horizontal"
@+id/imageView_item"
layout_marginLeft="
10dp"
@drawable/ic_launcher"
@+id/textView_item"
TextView"
接下来是main.xml布局:
.karl.list.MyListView
@+id/listView"
这里的MyListView是我们自定义的ListView,因为顶部的下拉刷新系统的ListView不能够实现,所以我们需要自定义ListView。
布局文件搞定,下面看下自定义的MyListView:
001
.karl.list;
002
003
importjava.text.SimpleDateFormat;
004
importjava.util.Date;
005
006
007
importandroid.content.Context;
008
importandroid.util.AttributeSet;
009
importandroid.util.Log;
010
importandroid.view.LayoutInflater;
011
importandroid.view.MotionEvent;
012
importandroid.view.View;
013
importandroid.view.ViewGroup;
014
importandroid.view.animation.LinearInterpolator;
015
importandroid.view.animation.RotateAnimation;
016
importandroid.widget.AbsListView;
017
importandroid.widget.BaseAdapter;
018
importandroid.widget.ImageView;
019
importandroid.widget.LinearLayout;
020
importandroid.widget.ListView;
021
importandroid.widget.AbsListView.OnScrollListener;
022
importandroid.widget.ProgressBar;
023
importandroid.widget.TextView;
024
025
publicclassMyListViewextendsListViewimplementsOnScrollListener{
026
027
privatestaticfinalStringTAG="
listview"
;
028
029
privatefinalstaticintRELEASE_To_REFRESH=0;
030
privatefinalstaticintPULL_To_REFRESH=1;
031
privatefinalstaticintREFRESHING=2;
032
privatefinalstaticintDONE=3;
033
privatefinalstaticintLOADING=4;
034
035
//实际的padding的距离与界面上偏移距离的比例
036
privatefinalstaticintRATIO=3;
037
038
privateLayoutInflaterinflater;
039
040
privateLinearLayoutheadView;
041
042
privateTextViewtipsTextview;
043
privateTextViewlastUpdatedTextView;
044
privateImageViewarrowImageView;
045
privateProgressBarprogressBar;
046
047
048
privateRotateAnimationanimation;
049
privateRotateAnimationreverseAnimation;
050
051
//用于保证startY的值在一个完整的touch事件中只被记录一次
052
privatebooleanisRecored;
053
054
privateintheadContentWidth;
055
privateintheadContentHeight;
056
057
privateintstartY;
058
privateintfirstItemIndex;
059
060
privateintstate;
061
062
privatebooleanisBack;
063
064
privateOnRefreshListenerrefreshListener;
065
066
privatebooleanisRefreshable;
067
068
publicMyListView(Contextcontext){
069
super(context);
070
init(context);
071
}
072
073
publicMyListView(Contextcontext,AttributeSetattrs){
074
super(context,attrs);
075
076
077
078
privatevoidinit(Contextcontext){
079
//setCacheColorHint(context.getResources().getColor(R.color.transparent));
080
inflater=LayoutInflater.from(context);
081
082
headView=(LinearLayout)inflater.inflate(R.layout.head,null);
083
084
arrowImageView=(ImageView)headView
085
.findViewById(R.id.head_arrowImageView);
086
arrowImageView.setMinimumWidth(70);
087
arrowImageView.setMinimumHeight(50);
088
progressBar=(ProgressBar)headView
089
.findViewById(R.id.head_progressBar);
090
tipsTextview=(TextView)headView.findViewById(R.id.head_tipsTextView);
091
lastUpdatedTextView=(TextView)headView
092
.findViewById(R.id.head_lastUpdatedTextView);
093
094
measureView(headView);
095
headContentHeight=headView.getMeasuredHeight();
096
headContentWidth=headView.getMeasuredWidth();
097
098
headView.setPadding(0,-1*headContentHeight,0,0);
099
headView.invalidate();
100
101
Log.v("
size"
"
width:
"
+headContentWidth+"
height:
102
+headContentHeight);
103
104
addHeaderView(headView,null,false);
105
setOnScrollListener(this);
106
107
animation=newRotateAnimation(0,-180,
108
RotateAnimation.RELATIVE_TO_SELF,0.5f,
109
RotateAnimation.RELATIVE_TO_SELF,0.5f);
110
animation.setInterpolator(newLinearInterpolator());
111
animation.setDuration(250);
112
animation.setFillAfter(true);
113
114
reverseAnimation=newRotateAnimation(-180,0,
115
116
117
reverseAnimation.setInterpolator(newLinearInterpolator());
118
reverseAnimation.setDuration(200);
119
reverseAnimation.setFillAfter(true);
120
121
state=DONE;
122
isRefreshable=false;
123
124
125
publicvoidonScroll(AbsListViewarg0,intfirstVisiableItem,intarg2,
126
intarg3){
127
firstItemIndex=firstVisiableItem;
128
129
130
publicvoidonScrollStateChanged(AbsListViewarg0,intarg1){
131
132
133
publicbooleanonTouchEvent(MotionEventevent){
134
135
if(isRefreshable){
136
switch(event.getAction()){
137
caseMotionEvent.ACTION_DOWN:
138
if(firstItemIndex==0&
&
!
isRecored){
139
isRecored=true;
140
startY=(int)event.getY();
141
Log.v(TAG,"
在down时候记录当前位置‘"
);
142
143
break;
144
145
caseMotionEvent.ACTION_UP:
146
147
if(state!
=REFRESHING&
state!
=LOADING){
148
if(state==DONE){
149
//什么都不做
150
151
if(state==PULL_To_REFRESH){
152
153
changeHeaderViewByState();
154
155
由下拉刷新状态,到done状态"
156
157
if(state==RELEASE_To_REFRESH){
158
state=REFRESHING;
159
160
onRefresh();