Android 仿新浪微博的ListView下拉更新功能.docx

上传人:b****2 文档编号:578856 上传时间:2023-04-29 格式:DOCX 页数:42 大小:179.47KB
下载 相关 举报
Android 仿新浪微博的ListView下拉更新功能.docx_第1页
第1页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第2页
第2页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第3页
第3页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第4页
第4页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第5页
第5页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第6页
第6页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第7页
第7页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第8页
第8页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第9页
第9页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第10页
第10页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第11页
第11页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第12页
第12页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第13页
第13页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第14页
第14页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第15页
第15页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第16页
第16页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第17页
第17页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第18页
第18页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第19页
第19页 / 共42页
Android 仿新浪微博的ListView下拉更新功能.docx_第20页
第20页 / 共42页
亲,该文档总共42页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Android 仿新浪微博的ListView下拉更新功能.docx

《Android 仿新浪微博的ListView下拉更新功能.docx》由会员分享,可在线阅读,更多相关《Android 仿新浪微博的ListView下拉更新功能.docx(42页珍藏版)》请在冰点文库上搜索。

Android 仿新浪微博的ListView下拉更新功能.docx

Android仿新浪微博的ListView下拉更新功能

Android仿新浪微博的ListView下拉更新功能

由于最近做的项目中也用到了这个功能,今天刚好实现了下,就趁现在有时间写篇博客分享下。

在做的时候也参考了下别人的代码,毕竟站在巨人的肩膀上才会是自己更加强大。

哈哈!

先看看新浪的下拉更新是什么样的吧!

OK,今天我们要实现的就是上面的下拉刷新功能。

首先实现下拉刷新的布局文件layout/head.xml

查看源码

打印?

01

xmlversion="1.0"encoding="utf-8"?

>

02

03

--ListView的头部-->

04

05

06

xmlns:

android="

07

android:

layout_width="fill_parent"

08

android:

layout_height="wrap_content"

09

android:

background="#ffffff"

10

>

11

12

--内容-->

13

14

android:

layout_width="fill_parent"

15

android:

layout_height="wrap_content"

16

android:

id="@+id/head_contentLayout"

17

android:

paddingLeft="30dp"

18

>

19

20

--箭头图像、进度条-->

21

22

android:

layout_width="wrap_content"

23

android:

layout_height="wrap_content"

24

android:

layout_alignParentLeft="true"

25

android:

layout_centerVertical="true"

26

>

27

28

--箭头-->

29

30

android:

layout_width="wrap_content"

31

android:

layout_height="wrap_content"

32

android:

layout_gravity="center"

33

android:

src="@drawable/arrow_down"

34

android:

id="@+id/head_arrowImageView"

35

/>

36

37

--进度条-->

38

39

android:

layout_width="wrap_content"

40

android:

layout_height="wrap_content"

41

style="?

android:

attr/progressBarStyleSmall"

42

android:

layout_gravity="center"

43

android:

id="@+id/head_progressBar"

44

45

android:

visibility="gone"

46

/>

47

48

49

50

--提示、最近更新-->

51

52

android:

layout_width="wrap_content"

53

android:

layout_height="wrap_content"

54

android:

layout_centerHorizontal="true"

55

android:

orientation="vertical"

56

android:

gravity="center_horizontal"

57

>

58

59

--提示-->

60

61

android:

layout_width="wrap_content"

62

android:

layout_height="wrap_content"

63

android:

text="下拉刷新"

64

android:

textSize="15dp"

65

android:

id="@+id/head_tipsTextView"

66

/>

67

68

--最近更新-->

69

70

android:

layout_width="wrap_content"

71

android:

layout_height="wrap_content"

72

android:

id="@+id/head_lastUpdatedTextView"

73

android:

text="上次更新"

74

android:

textSize="12dp"

75

/>

76

77

78

79

80

81

82

83

然后设置item的布局模板item.xml:

查看源码

打印?

01

xmlversion="1.0"encoding="utf-8"?

>

02

android="

03

android:

layout_width="match_parent"

04

android:

layout_height="wrap_content"

05

android:

gravity="center_vertical"

06

android:

background="#ffffff"

07

android:

orientation="horizontal">

08

09

10

android:

id="@+id/imageView_item"

11

android:

layout_width="wrap_content"

12

android:

layout_height="wrap_content"

13

android:

layout_marginLeft="10dp"

14

android:

src="@drawable/ic_launcher"/>

15

16

17

android:

id="@+id/textView_item"

18

android:

layout_width="wrap_content"

19

android:

layout_height="wrap_content"

20

android:

layout_marginLeft="10dp"

21

android:

text="TextView"/>

22

23

接下来是main.xml布局:

查看源码

打印?

01

xmlversion="1.0"encoding="utf-8"?

>

02

android="

03

android:

layout_width="fill_parent"

04

android:

layout_height="fill_parent"

05

android:

orientation="vertical">

06

07

<.karl.list.MyListView

08

android:

layout_width="fill_parent"

09

android:

layout_height="fill_parent"

10

android:

id="@+id/listView"

11

/>

12

13

14

这里的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

init(context);

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

RotateAnimation.RELATIVE_TO_SELF,0.5f,

116

RotateAnimation.RELATIVE_TO_SELF,0.5f);

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

state=DONE;

153

changeHeaderViewByState();

154

155

Log.v(TAG,"由下拉刷新状态,到done状态");

156

}

157

if(state==RELEASE_To_REFRESH){

158

state=REFRESHING;

159

changeHeaderViewByState();

160

onRefresh();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2