转自http://www.linuxidc.com/Linux/2012-08/67207.htm

主布局main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <HorizontalScrollView
  7. android:id="@+id/horizontalscrollview"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:background="@drawable/item"
  11. android:fadingEdge="@null"
  12. android:scrollbars="none">
  13. <LinearLayout
  14. android:id="@+id/linearlayout"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:orientation="horizontal"
  18. >
  19. </LinearLayout>
  20. </HorizontalScrollView>
  21. <android.support.v4.view.ViewPager
  22. android:id="@+id/viewpaper"
  23. android:layout_width="fill_parent"
  24. android:layout_height="match_parent"
  25. android:layout_weight="1">
  26. </android.support.v4.view.ViewPager>
  27. </LinearLayout>

2,接着看看QQonline.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="@drawable/image2">
  7. <ExpandableListView
  8. android:id="@+id/elistview"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginLeft="5dp"
  12. />
  13. </LinearLayout>

3.接着看看下拉更新的布局pullrefresh.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@drawable/image1"
  6. android:orientation="vertical">
  7. <com.wang.pullrefresh.MyListView
  8. android:id="@+id/listView"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"/>
  11. </LinearLayout>
4.下拉更新过程实现的布局refresh.xml
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content">
  5. <RelativeLayout
  6. android:id="@+id/head_contentLayout"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:paddingLeft="30dp">
  10. <FrameLayout
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentLeft="true"
  14. android:layout_centerVertical="true">
  15. <ImageView
  16. android:id="@+id/head_arrowImageView"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_gravity="center"
  20. android:src="@drawable/down"/>
  21. <ProgressBar
  22. android:id="@+id/head_progressBar"
  23. style="?android:attr/progressBarStyleSmall"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_gravity="center"
  27. android:visibility="gone"/>
  28. </FrameLayout>
  29. <LinearLayout
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_centerHorizontal="true"
  33. android:gravity="center_horizontal"
  34. android:orientation="vertical">
  35. <TextView
  36. android:id="@+id/head_tipsTextView"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="下拉可以刷新"
  40. android:textSize="15dp"/>
  41. <TextView
  42. android:id="@+id/head_lastUpdatedTextView"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="上次更新时间:"
  46. android:textSize="12dp"/>
  47. </LinearLayout>
  48. </RelativeLayout>
  49. </LinearLayout>
5.下拉更新的列表显示的布局list_item.xml
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:gravity="center_vertical"
  6. android:orientation="horizontal">
  7. <ImageView
  8. android:id="@+id/imageView_item"
  9. android:layout_width="60dp"
  10. android:layout_height="60dp"
  11. android:layout_marginLeft="5dp"
  12. android:src="@drawable/qq"/>
  13. <TextView
  14. android:id="@+id/textView_item"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_marginLeft="10dp"
  18. android:textColor="#FFFFFF"
  19. />
  20. </LinearLayout>
6.接着看看还没有完善的QQ聊天信息的布局main3.xml
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="@drawable/image3">
  7. </LinearLayout>

7.接着看看主活动的实现过程ViewPaper1Activity.java

  1. packagecom.wang;
  2. importjava.util.ArrayList;
  3. importandroid.app.ActivityGroup;
  4. importandroid.content.Intent;
  5. importandroid.graphics.Bitmap;
  6. importandroid.graphics.BitmapFactory;
  7. importandroid.graphics.drawable.BitmapDrawable;
  8. importandroid.os.Bundle;
  9. importandroid.support.v4.view.PagerAdapter;
  10. importandroid.support.v4.view.ViewPager;
  11. importandroid.support.v4.view.ViewPager.OnPageChangeListener;
  12. importandroid.view.Gravity;
  13. importandroid.view.View;
  14. importandroid.view.Window;
  15. importandroid.view.WindowManager;
  16. importandroid.view.View.OnClickListener;
  17. importandroid.widget.HorizontalScrollView;
  18. importandroid.widget.LinearLayout;
  19. importandroid.widget.RadioButton;
  20. importandroid.widget.RadioGroup;
  21. importandroid.widget.TextView;
  22. importandroid.widget.LinearLayout.LayoutParams;
  23. publicclassViewPaper1ActivityextendsActivityGroupimplements
  24. OnClickListener{
  25. privateRadioGroupradioGroup;
  26. privateStringarray[]={"QQ空间","QQ","QQ消息"};
  27. privateLinearLayoutlinearLayout;
  28. privateArrayList<TextView>textViews;
  29. privateViewPagerviewPager;
  30. privateArrayList<View>pageViews;
  31. privateHorizontalScrollViewhorizontalScrollView;
  32. /***
  33. *初始化视图
  34. *
  35. *
  36. */
  37. voidInItView(){
  38. //构造一个新的ArrayList实例对象
  39. pageViews=newArrayList<View>();
  40. /**
  41. *  开始一个新的活动中运行的组织。每一个活动你开始必须有一个独一无二的字符串标识与其相关联
  42. ***/
  43. Viewview1=getLocalActivityManager().startActivity("main1",
  44. newIntent(this,pullrefreshtest.class)).getDecorView();
  45. Viewview2=getLocalActivityManager().startActivity("main2",
  46. newIntent(this,QQonline.class)).getDecorView();
  47. Viewview3=getLocalActivityManager().startActivity("main3",
  48. newIntent(this,main3.class)).getDecorView();
  49. //添加指定的对象在文章末尾的ArrayList。
  50. pageViews.add(view1);
  51. pageViews.add(view2);
  52. pageViews.add(view3);
  53. }
  54. /***
  55. *初始化话标题栏
  56. */
  57. voidInItTitle(){
  58. //获取窗口管理器显示自定义窗口,去掉默认显示对象。得到1/3屏幕宽度
  59. intwidth=getWindowManager().getDefaultDisplay().getWidth()/3;
  60. for(inti=0;i<array.length;i++){
  61. //声明一个radioButton对象
  62. RadioButtonradioButton=newRadioButton(this,null);
  63. radioButton.setText(array[i]);
  64. radioButton.setWidth(width);
  65. radioButton.setHeight(70);
  66. //集水平对齐文本和垂直重力的时候将会使用有额外的空间在TextView超出要求的文本本身
  67. radioButton.setGravity(Gravity.CENTER);
  68. //添加子视图。如果没有布局参数对孩子已经设置,默认参数对于这个ViewGroup上设置的孩子。
  69. radioGroup.addView(radioButton);
  70. }
  71. }
  72. /***
  73. *初始化文本
  74. */
  75. voidinitTextView(){
  76. //声明一个ArrayList对象
  77. textViews=newArrayList<TextView>();
  78. //获取窗口管理器显示自定义窗口,去掉默认显示对象。得到1/3屏幕宽度
  79. intwidth=getWindowManager().getDefaultDisplay().getWidth()/3;
  80. intheight=60;
  81. for(inti=0;i<array.length;i++){
  82. TextViewtextView=newTextView(this);
  83. textView.setText(array[i]);
  84. textView.setTextSize(17);
  85. textView.setWidth(width);
  86. textView.setHeight(height-30);
  87. textView.setGravity(Gravity.CENTER);
  88. textView.setId(i);
  89. //设置文本的监听事件
  90. textView.setOnClickListener(this);
  91. textViews.add(textView);
  92. LinearLayout.LayoutParamslayoutParams=newLayoutParams(
  93. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  94. layoutParams.width=1;
  95. layoutParams.height=height-40;
  96. layoutParams.gravity=Gravity.CENTER;
  97. //添加子视图。如果没有布局参数对孩子已经设置,默认参数对于这个ViewGroup上设置的孩子。
  98. linearLayout.addView(textView);
  99. }
  100. }
  101. /***
  102. *选中后的处理
  103. */
  104. publicvoidselect(intid){
  105. for(inti=0;i<array.length;i++){
  106. if(id==i){
  107. //得到图片的资源
  108. Bitmapbitmap=BitmapFactory.decodeResource(getResources(),
  109. R.drawable.itembg);
  110. //设置背景图
  111. textViews.get(id).setBackgroundDrawable(
  112. newBitmapDrawable(bitmap));
  113. viewPager.setCurrentItem(i);
  114. }
  115. else{
  116. textViews.get(i).setBackgroundDrawable(newBitmapDrawable());
  117. }
  118. }
  119. }
  120. @Override
  121. publicvoidonCreate(BundlesavedInstanceState){
  122. super.onCreate(savedInstanceState);
  123. //去除标题栏
  124. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  125. //取消状态栏,充满全屏
  126. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  127. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  128. setContentView(R.layout.main);
  129. //实例化组件
  130. linearLayout=(LinearLayout)findViewById(R.id.linearlayout);
  131. viewPager=(ViewPager)findViewById(R.id.viewpaper);
  132. horizontalScrollView=(HorizontalScrollView)findViewById(R.id.horizontalscrollview);
  133. //调用方法实现
  134. initTextView();
  135. select(0);
  136. InItView();
  137. viewPager.setAdapter(newPagerAdapter(){
  138. //得到数目
  139. publicintgetCount(){
  140. returnpageViews.size();
  141. }
  142. @Override
  143. publicbooleanisViewFromObject(Viewview,Objectobject){
  144. returnview==object;
  145. }
  146. @Override
  147. publicintgetItemPosition(Objectobject){
  148. //TODOAuto-generatedmethodstub
  149. returnsuper.getItemPosition(object);
  150. }
  151. @Override
  152. publicvoiddestroyItem(Viewview,intid,Objectarg2){
  153. //TODOAuto-generatedmethodstub
  154. ((ViewPager)view).removeView(pageViews.get(id));
  155. }
  156. //获取每一个item的id
  157. @Override
  158. publicObjectinstantiateItem(Viewview,intid){
  159. ((ViewPager)view).addView(pageViews.get(id));
  160. returnpageViews.get(id);
  161. }
  162. });
  163. //页面改变时候的监听事件
  164. viewPager.setOnPageChangeListener(newOnPageChangeListener(){
  165. @Override
  166. publicvoidonPageSelected(intarg0){
  167. select(arg0);
  168. }
  169. @Override
  170. publicvoidonPageScrolled(intarg0,floatarg1,intarg2){
  171. }
  172. @Override
  173. publicvoidonPageScrollStateChanged(intarg0){
  174. }
  175. });
  176. }
  177. @Override
  178. publicvoidonClick(Viewv){
  179. select(v.getId());
  180. }
  181. }

8.接着看看QQonline.java 的实现

  1. packagecom.wang;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.ContextMenu;
  5. importandroid.view.View;
  6. importandroid.view.Window;
  7. importandroid.view.WindowManager;
  8. importandroid.view.ContextMenu.ContextMenuInfo;
  9. importandroid.widget.ExpandableListAdapter;
  10. importandroid.widget.ExpandableListView;
  11. importandroid.widget.Toast;
  12. importandroid.widget.ExpandableListView.OnChildClickListener;
  13. importandroid.widget.ExpandableListView.OnGroupClickListener;
  14. importandroid.widget.ExpandableListView.OnGroupCollapseListener;
  15. importandroid.widget.ExpandableListView.OnGroupExpandListener;
  16. publicclassQQonlineextendsActivity{
  17. //创建一个上下文菜单的方法
  18. publicvoidonCreateContextMenu(ContextMenumenu,Viewv,
  19. ContextMenuInfomenuInfo){
  20. super.onCreateContextMenu(menu,v,menuInfo);
  21. //一个垂直滚动的两级列表。取得菜单项
  22. ExpandableListView.ExpandableListContextMenuInfoinfo=(ExpandableListView.ExpandableListContextMenuInfo)menuInfo;
  23. //获得这个类型的位置
  24. inttype=ExpandableListView
  25. .getPackedPositionType(info.packedPosition);
  26. //取得所在组的索引
  27. intgroup=ExpandableListView
  28. .getPackedPositionGroup(info.packedPosition);
  29. //取得子菜单的索引
  30. intchild=ExpandableListView
  31. .getPackedPositionGroup(info.packedPosition);
  32. Toast.makeText(QQonline.this,
  33. "类型="+type+"分组:"+group+"子选项:"+child,
  34. Toast.LENGTH_LONG).show();
  35. }
  36. privateExpandableListVieweListView=null;
  37. privateExpandableListAdapteradapter=null;
  38. @Override
  39. publicvoidonCreate(BundlesavedInstanceState){
  40. //去除标题,
  41. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  42. //取消状态栏,充满全屏
  43. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  44. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  45. super.onCreate(savedInstanceState);
  46. super.setContentView(R.layout.qqonline);
  47. //实例化组件
  48. this.eListView=(ExpandableListView)findViewById(R.id.elistview);
  49. //声明一个adapter对象
  50. adapter=newmyExpendableadapler(this);
  51. //设置适配器提供了数据
  52. this.eListView.setAdapter(this.adapter);
  53. // 注册一个上下文菜单显示给定的视图(多个视图可以显示上下文菜单)。
  54. super.registerForContextMenu(this.eListView);
  55. //设置点击时候触发的事件1,子选项点击事件2。父选项单击事件3.分组打开事件4.分组关闭事件
  56. this.eListView.setOnChildClickListener(newChildClickListener());
  57. this.eListView.setOnGroupClickListener(newGroupClickListener());
  58. this.eListView.setOnGroupExpandListener(newGroupExpandListener());
  59. this.eListView.setOnGroupCollapseListener(newGroupCollapseListener());
  60. }
  61. ///1,子选项点击事件
  62. privateclassChildClickListenerimplementsOnChildClickListener{
  63. publicbooleanonChildClick(ExpandableListViewparent,Viewv,
  64. intgroupPosition,intchildPosition,longid){
  65. Toast
  66. .makeText(
  67. QQonline.this,
  68. "子选项被选中,所的组:"+groupPosition+"子选项的位置:"
  69. +childPosition,Toast.LENGTH_LONG).show();
  70. returnfalse;
  71. }
  72. }
  73. //2。父选项单击事件
  74. privateclassGroupClickListenerimplementsOnGroupClickListener{
  75. publicbooleanonGroupClick(ExpandableListViewparent,Viewv,
  76. intgroupPosition,longid){
  77. Toast.makeText(QQonline.this,"分组选项被选中,所在组:"+groupPosition,
  78. Toast.LENGTH_LONG).show();
  79. returnfalse;
  80. }
  81. }
  82. //3.分组打开事件
  83. privateclassGroupExpandListenerimplementsOnGroupExpandListener{
  84. publicvoidonGroupExpand(intgroupPosition){
  85. //TODOAuto-generatedmethodstub
  86. Toast.makeText(QQonline.this,"打开分组,所在组:"+groupPosition,
  87. Toast.LENGTH_LONG).show();
  88. }
  89. }
  90. //4.分组关闭事件
  91. privateclassGroupCollapseListenerimplementsOnGroupCollapseListener{
  92. publicvoidonGroupCollapse(intgroupPosition){
  93. Toast.makeText(QQonline.this,"关闭分组,所在组:"+groupPosition,
  94. Toast.LENGTH_LONG).show();
  95. }
  96. }
  97. }
9.接着需要myExpendableadapler.java
  1. packagecom.wang;
  2. importandroid.content.Context;
  3. importandroid.view.Gravity;
  4. importandroid.view.View;
  5. importandroid.view.ViewGroup;
  6. importandroid.widget.AbsListView;
  7. importandroid.widget.BaseExpandableListAdapter;
  8. importandroid.widget.TextView;
  9. publicclassmyExpendableadaplerextendsBaseExpandableListAdapter{
  10. //组名称
  11. privateString[]group=newString[]{"在线好友","高中同学","南阳理工同学","寝室哥们"};
  12. //子选项的名字
  13. privateString[][]child=newString[][]{
  14. {"李彦宏","马云","雷军","周鸿祎","唐骏","赵威威","李冕","马海刚","李斌","刘洪刚",
  15. "马玉龙","刘慧敏","王文龙"},
  16. {"赵威威","李冕","马海刚","李斌","刘洪刚","马玉龙","刘慧敏","王文龙"},
  17. {"张紫阳","王亚光","刘一鸣","崔芳芳","董卫杰","刘永乐"},
  18. {"武云册","王俊伟","王杰","付永振","杜新峰","应志超"}};
  19. privateContextcontext=null;
  20. //构造函数
  21. publicmyExpendableadapler(Contextcontext){
  22. this.context=context;
  23. }
  24. publicObjectgetChild(intgroupPosition,intchildPosition){
  25. returnthis.child[groupPosition][childPosition];
  26. }
  27. publiclonggetChildId(intgroupPosition,intchildPosition){
  28. returnchildPosition;
  29. }
  30. privateTextViewbuildTextView(){
  31. //LayoutParamsAbsListView扩展提供一个位置来保存视图类型。
  32. AbsListView.LayoutParamsparams=newAbsListView.LayoutParams(
  33. ViewGroup.LayoutParams.FILL_PARENT,40);
  34. TextViewtextView=newTextView(this.context);
  35. textView.setLayoutParams(params);
  36. //大小
  37. textView.setTextSize(15.0f);
  38. textView.setGravity(Gravity.LEFT+3);
  39. textView.setPadding(40,8,3,3);
  40. returntextView;
  41. }
  42. publicViewgetChildView(intgroupPosition,intchildPosition,
  43. booleanisLastChild,ViewconvertView,ViewGroupparent){
  44. TextViewtextView=newTextView(this.context);
  45. //得到每组的子选项并转换成字符串
  46. textView
  47. .setText(this.getChild(groupPosition,childPosition).toString());
  48. returntextView;
  49. }
  50. //统计子选项的个数
  51. publicintgetChildrenCount(intgroupPosition){
  52. //TODOAuto-generatedmethodstub
  53. returnthis.child[groupPosition].length;
  54. }
  55. //得到复选项的位置
  56. publicObjectgetGroup(intgroupPosition){
  57. //TODOAuto-generatedmethodstub
  58. returnthis.group[groupPosition];
  59. }
  60. //得到复选项的个数
  61. publicintgetGroupCount(){
  62. //TODOAuto-generatedmethodstub
  63. returnthis.group.length;
  64. }
  65. //得到复选项的id
  66. publiclonggetGroupId(intgroupPosition){
  67. //TODOAuto-generatedmethodstub
  68. returngroupPosition;
  69. }
  70. publicViewgetGroupView(intgroupPosition,booleanisExpanded,
  71. ViewconvertView,ViewGroupparent){
  72. //TODOAuto-generatedmethodstub
  73. TextViewtextView=this.buildTextView();
  74. textView.setText(this.getGroup(groupPosition).toString());
  75. returntextView;
  76. }
  77. //是否子选项和父选项id是稳定在改变底层数据。
  78. publicbooleanhasStableIds(){
  79. //TODOAuto-generatedmethodstub
  80. returntrue;
  81. }
  82. //p判断子选项是否可以选择
  83. publicbooleanisChildSelectable(intgroupPosition,intchildPosition){
  84. //TODOAuto-generatedmethodstub
  85. returntrue;
  86. }
  87. }

10.有两个界面还没有添加功能pullrefreshtest.java

  1. packagecom.wang;
  2. importjava.util.LinkedList;
  3. importcom.wang.pullrefresh.MyListView;
  4. importcom.wang.pullrefresh.MyListView.OnRefreshListener;
  5. importandroid.app.Activity;
  6. importandroid.os.AsyncTask;
  7. importandroid.os.Bundle;
  8. importandroid.view.LayoutInflater;
  9. importandroid.view.View;
  10. importandroid.view.ViewGroup;
  11. importandroid.view.Window;
  12. importandroid.view.WindowManager;
  13. importandroid.widget.BaseAdapter;
  14. importandroid.widget.TextView;
  15. publicclasspullrefreshtestextendsActivity{
  16. privateLinkedList<String>data;
  17. privateBaseAdapteradapter;
  18. publicvoidonCreate(BundlesavedInstanceState){
  19. //去除标题栏
  20. requestWindowFeature(Window.FEATURE_NO_TITLE);
  21. //设置全屏,取消状态栏
  22. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  23. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.pullrefresh);
  26. data=newLinkedList<String>();
  27. for(inti=40;i<50;i++){
  28. //输出list——item上的数据
  29. data.add(String.valueOf("暑假第"+i+"天,在南阳理工,想家ing..."));
  30. }
  31. finalMyListViewlistView=(MyListView)findViewById(R.id.listView);
  32. adapter=newBaseAdapter(){
  33. //得到一个视图,显示在指定位置上的数据在数据集,可以创建一个视图从XML布局文件
  34. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  35. //上下文全局应用程序对象
  36. convertView=LayoutInflater.from(getApplicationContext())
  37. .inflate(R.layout.list_item,null);
  38. //实例化组件
  39. TextViewtextView=(TextView)convertView
  40. .findViewById(R.id.textView_item);
  41. //设置文本本内容
  42. textView.setText(data.get(position));
  43. returnconvertView;
  44. }
  45. //得到行相关联id列表中指定的位置。
  46. publiclonggetItemId(intposition){
  47. returnposition;
  48. }
  49. //获得相关的数据项中的指定位置的数据集
  50. publicObjectgetItem(intposition){
  51. returndata.get(position);
  52. }
  53. //获得项目在数据集适配器的个数。
  54. publicintgetCount(){
  55. returndata.size();
  56. }
  57. };
  58. listView.setAdapter(adapter);
  59. listView.setonRefreshListener(newOnRefreshListener(){
  60. publicvoidonRefresh(){
  61. newAsyncTask<Void,Void,Void>(){
  62. //...b表示多个参数
  63. protectedVoiddoInBackground(Void...params){
  64. try{
  65. //
  66. Thread.sleep(1000);
  67. }catch(Exceptione){
  68. e.printStackTrace();
  69. }
  70. //增加一条数据到list中
  71. data.addFirst("刷新后内容:每天都是新的一天!!!,親!要努力奋斗哦!!!");
  72. returnnull;
  73. }
  74. protectedvoidonPostExecute(Voidresult){
  75. adapter.notifyDataSetChanged();
  76. listView.onRefreshComplete();
  77. }
  78. }.execute(null);
  79. }
  80. });
  81. }
  82. }

11.接着是下拉列表更新的实现过程MyListView.java

  1. packagecom.wang.pullrefresh;
  2. importjava.text.SimpleDateFormat;
  3. importjava.util.Date;
  4. importcom.wang.R;
  5. importandroid.content.Context;
  6. importandroid.util.AttributeSet;
  7. importandroid.util.Log;
  8. importandroid.view.LayoutInflater;
  9. importandroid.view.MotionEvent;
  10. importandroid.view.View;
  11. importandroid.view.ViewGroup;
  12. importandroid.view.View.MeasureSpec;
  13. importandroid.view.animation.LinearInterpolator;
  14. importandroid.view.animation.RotateAnimation;
  15. importandroid.widget.AbsListView;
  16. importandroid.widget.BaseAdapter;
  17. importandroid.widget.ImageView;
  18. importandroid.widget.LinearLayout;
  19. importandroid.widget.ListView;
  20. importandroid.widget.ProgressBar;
  21. importandroid.widget.TextView;
  22. importandroid.widget.AbsListView.OnScrollListener;
  23. publicclassMyListViewextendsListViewimplementsOnScrollListener{
  24. privatestaticfinalStringTAG="listview";
  25. privatefinalstaticintRELEASE_To_REFRESH=0;
  26. privatefinalstaticintPULL_To_REFRESH=1;
  27. privatefinalstaticintREFRESHING=2;
  28. privatefinalstaticintDONE=3;
  29. privatefinalstaticintLOADING=4;
  30. //实际的padding的距离与界面上偏移距离的比例
  31. privatefinalstaticintRATIO=3;
  32. privateLayoutInflaterinflater;
  33. privateLinearLayoutheadView;
  34. privateTextViewtipsTextview;
  35. privateTextViewlastUpdatedTextView;
  36. privateImageViewarrowImageView;
  37. privateProgressBarprogressBar;
  38. privateRotateAnimationanimation;
  39. privateRotateAnimationreverseAnimation;
  40. //用于保证startY的值在一个完整的touch事件中只被记录一次
  41. privatebooleanisRecored;
  42. privateintheadContentWidth;
  43. privateintheadContentHeight;
  44. privateintstartY;
  45. privateintfirstItemIndex;
  46. privateintstate;
  47. privatebooleanisBack;
  48. privateOnRefreshListenerrefreshListener;
  49. privatebooleanisRefreshable;
  50. publicMyListView(Contextcontext){
  51. super(context);
  52. //调用下面初始化的函数
  53. init(context);
  54. }
  55. publicMyListView(Contextcontext,AttributeSetattrs){
  56. super(context,attrs);
  57. //调用下面初始化的函数
  58. init(context);
  59. }
  60. privatevoidinit(Contextcontext){
  61. //获得LayoutInflater从给定的上下文。
  62. inflater=LayoutInflater.from(context);
  63. //实例化布局XML文件转换成相应的视图对象。
  64. headView=(LinearLayout)inflater.inflate(R.layout.refresh,null);
  65. arrowImageView=(ImageView)headView
  66. .findViewById(R.id.head_arrowImageView);
  67. //设置最小宽度和高度
  68. arrowImageView.setMinimumWidth(70);
  69. arrowImageView.setMinimumHeight(50);
  70. //实例化布局XML文件转换成相应的视图对象。
  71. progressBar=(ProgressBar)headView
  72. .findViewById(R.id.head_progressBar);
  73. tipsTextview=(TextView)headView.findViewById(R.id.head_tipsTextView);
  74. lastUpdatedTextView=(TextView)headView
  75. .findViewById(R.id.head_lastUpdatedTextView);
  76. //调用下拉刷新的方法
  77. measureView(headView);
  78. //d得到原始高度和宽度
  79. headContentHeight=headView.getMeasuredHeight();
  80. headContentWidth=headView.getMeasuredWidth();
  81. //设置填充。视图可能添加的空间要求显示滚动条,这取决于风格和知名度的滚动条
  82. headView.setPadding(0,-1*headContentHeight,0,0);
  83. headView.invalidate();
  84. //标签用来识别一个日志消息的来源。标识类或活动日志调用发生
  85. Log.v("size","width:"+headContentWidth+"height:"
  86. +headContentHeight);
  87. //添加一个固定视图出现在列表的顶部
  88. addHeaderView(headView,null,false);
  89. //设置监听事件
  90. setOnScrollListener(this);
  91. //动画效果实现下拉和松开时候图片的180度旋转注意0,-180,和他是有区别的-180,0,
  92. animation=newRotateAnimation(0,-180,
  93. RotateAnimation.RELATIVE_TO_SELF,0.5f,
  94. RotateAnimation.RELATIVE_TO_SELF,0.5f);
  95. //设置加速度曲线为这个动画。默认值为一个线性插值。
  96. animation.setInterpolator(newLinearInterpolator());
  97. animation.setDuration(300);
  98. //如果fillAfter是真的,转换,该动画执行完成时将会持续下去
  99. animation.setFillAfter(true);
  100. reverseAnimation=newRotateAnimation(-180,0,
  101. RotateAnimation.RELATIVE_TO_SELF,0.5f,
  102. RotateAnimation.RELATIVE_TO_SELF,0.5f);
  103. //设置加速度曲线为这个动画。默认值为一个线性插值。
  104. reverseAnimation.setInterpolator(newLinearInterpolator());
  105. reverseAnimation.setDuration(300);
  106. //如果fillAfter是真的,转换,该动画执行完成时将会持续下去
  107. reverseAnimation.setFillAfter(true);
  108. //设置状态
  109. state=DONE;
  110. //设置不可刷新状态
  111. isRefreshable=false;
  112. }
  113. //回调方法时要调用的列表或网格已经滚动。这将完成之后调用的滚动方法
  114. publicvoidonScroll(AbsListViewarg0,intfirstVisiableItem,intarg2,
  115. intarg3){
  116. firstItemIndex=firstVisiableItem;
  117. }
  118. /*
  119. *回调方法调用而列表视图或网格视图被滚动。如果这个视图被滚动,将调用此方法在接下来的一局画卷的呈现*
  120. */
  121. publicvoidonScrollStateChanged(AbsListViewarg0,intarg1){
  122. }
  123. //触摸事件监听
  124. publicbooleanonTouchEvent(MotionEventevent){
  125. //判断是否可以刷新
  126. if(isRefreshable){
  127. //根据动作相应不同的方法
  128. switch(event.getAction()){
  129. //当按住屏幕向下拉屏幕的时候
  130. caseMotionEvent.ACTION_DOWN:
  131. if(firstItemIndex==0&&!isRecored){
  132. isRecored=true;
  133. startY=(int)event.getY();
  134. Log.v(TAG,"在下拉的时候记录当前位置‘");
  135. }
  136. break;
  137. //当按住屏幕向上松屏幕的时候
  138. caseMotionEvent.ACTION_UP:
  139. if(state!=REFRESHING&&state!=LOADING){
  140. if(state==DONE){
  141. }
  142. if(state==PULL_To_REFRESH){
  143. state=DONE;
  144. changeHeaderViewByState();
  145. Log.v(TAG,"由下拉刷新状态,到done状态");
  146. }
  147. if(state==RELEASE_To_REFRESH){
  148. state=REFRESHING;
  149. changeHeaderViewByState();
  150. onRefresh();
  151. Log.v(TAG,"由松开刷新状态,到done状态");
  152. }
  153. }
  154. isRecored=false;
  155. isBack=false;
  156. break;
  157. //当按住屏幕移动时候
  158. caseMotionEvent.ACTION_MOVE:
  159. inttempY=(int)event.getY();
  160. if(!isRecored&&firstItemIndex==0){
  161. Log.v(TAG,"在move时候记录下位置");
  162. isRecored=true;
  163. startY=tempY;
  164. }
  165. if(state!=REFRESHING&&isRecored&&state!=LOADING){
  166. /***
  167. *,当前的位置一直是在head,否则如果当列表超出屏幕的话,当在上推的时候,列表会同时进行滚动
  168. */
  169. //可以松手去刷新了
  170. if(state==RELEASE_To_REFRESH){
  171. setSelection(0);
  172. //往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步
  173. if(((tempY-startY)/RATIO<headContentHeight)
  174. &&(tempY-startY)>0){
  175. state=PULL_To_REFRESH;
  176. changeHeaderViewByState();
  177. Log.v(TAG,"由松开刷新状态转变到下拉刷新状态");
  178. }
  179. //一下子推到顶了
  180. elseif(tempY-startY<=0){
  181. state=DONE;
  182. //调用改变时候的方法,更新UI
  183. changeHeaderViewByState();
  184. Log.v(TAG,"由松开刷新状态转变到done状态");
  185. }else{
  186. }
  187. }
  188. //还没有到达显示松开刷新的时候
  189. if(state==PULL_To_REFRESH){
  190. setSelection(0);
  191. //下拉到可以进入RELEASE_TO_REFRESH的状态
  192. if((tempY-startY)/RATIO>=headContentHeight){
  193. state=RELEASE_To_REFRESH;
  194. isBack=true;
  195. //调用改变时候的方法,更新UI
  196. changeHeaderViewByState();
  197. Log.v(TAG,"由done或者下拉刷新状态转变到松开刷新");
  198. }
  199. //上推到顶了
  200. elseif(tempY-startY<=0){
  201. state=DONE;
  202. //调用改变时候的方法,更新UI
  203. changeHeaderViewByState();
  204. Log.v(TAG,"由DOne或者下拉刷新状态转变到done状态");
  205. }
  206. }
  207. //done状态下
  208. if(state==DONE){
  209. if(tempY-startY>0){
  210. state=PULL_To_REFRESH;
  211. //调用改变时候的方法,更新UI
  212. changeHeaderViewByState();
  213. }
  214. }
  215. //更新headView的size
  216. if(state==PULL_To_REFRESH){
  217. headView.setPadding(0,-1*headContentHeight
  218. +(tempY-startY)/RATIO,0,0);
  219. }
  220. //更新headView的paddingTop
  221. if(state==RELEASE_To_REFRESH){
  222. headView.setPadding(0,(tempY-startY)/RATIO
  223. -headContentHeight,0,0);
  224. }
  225. }
  226. break;
  227. }
  228. }
  229. returnsuper.onTouchEvent(event);
  230. }
  231. //当状态改变时候,调用该方法,以更新界面
  232. privatevoidchangeHeaderViewByState(){
  233. //根据当前的状态进行判断
  234. switch(state){
  235. //下拉时候,松开既可刷新
  236. caseRELEASE_To_REFRESH:
  237. //设置视图VISIBLE可见,GONE不可见
  238. arrowImageView.setVisibility(View.VISIBLE);
  239. progressBar.setVisibility(View.GONE);
  240. tipsTextview.setVisibility(View.VISIBLE);
  241. lastUpdatedTextView.setVisibility(View.VISIBLE);
  242. //现在开始指定的动画。
  243. arrowImageView.clearAnimation();
  244. arrowImageView.startAnimation(animation);
  245. tipsTextview.setText("松开既可刷新");
  246. Log.v(TAG,"当前状态,松开即可刷新");
  247. break;
  248. //开始时候,下拉刷新
  249. casePULL_To_REFRESH:
  250. //设置视图VISIBLE可见,GONE不可见
  251. progressBar.setVisibility(View.GONE);
  252. tipsTextview.setVisibility(View.VISIBLE);
  253. lastUpdatedTextView.setVisibility(View.VISIBLE);
  254. //现在开始指定的动画。
  255. arrowImageView.clearAnimation();
  256. arrowImageView.setVisibility(View.VISIBLE);
  257. if(isBack){
  258. isBack=false;
  259. //现在开始指定的动画。
  260. arrowImageView.clearAnimation();
  261. arrowImageView.startAnimation(reverseAnimation);
  262. tipsTextview.setText("下拉刷新");
  263. }else{
  264. tipsTextview.setText("下拉刷新");
  265. }
  266. Log.v(TAG,"当前状态,下拉刷新");
  267. break;
  268. caseREFRESHING:
  269. headView.setPadding(0,0,0,0);
  270. //设置视图VISIBLE可见,GONE不可见
  271. progressBar.setVisibility(View.VISIBLE);
  272. //现在开始指定的动画。
  273. arrowImageView.clearAnimation();
  274. arrowImageView.setVisibility(View.GONE);
  275. tipsTextview.setText("正在刷新...");
  276. lastUpdatedTextView.setVisibility(View.VISIBLE);
  277. Log.v(TAG,"当前状态,正在刷新...");
  278. break;
  279. caseDONE:
  280. //设置填充。视图可能添加的空间要求显示滚动条
  281. headView.setPadding(0,-1*headContentHeight,0,0);
  282. //设置视图VISIBLE可见,GONE不可见
  283. progressBar.setVisibility(View.GONE);
  284. //现在开始指定的动画。
  285. arrowImageView.clearAnimation();
  286. arrowImageView.setImageResource(R.drawable.down);
  287. tipsTextview.setText("下拉刷新");
  288. lastUpdatedTextView.setVisibility(View.VISIBLE);
  289. Log.v(TAG,"当前状态");
  290. break;
  291. }
  292. }
  293. publicvoidsetonRefreshListener(OnRefreshListenerrefreshListener){
  294. this.refreshListener=refreshListener;
  295. isRefreshable=true;
  296. }
  297. publicinterfaceOnRefreshListener{
  298. publicvoidonRefresh();
  299. }
  300. //设置更新时间
  301. publicvoidonRefreshComplete(){
  302. state=DONE;
  303. //
  304. SimpleDateFormatformat=newSimpleDateFormat("yyyy年MM月dd日HH:mm");
  305. Stringdate=format.format(newDate());
  306. lastUpdatedTextView.setText("最近更新:"+date);
  307. changeHeaderViewByState();
  308. }
  309. privatevoidonRefresh(){
  310. if(refreshListener!=null){
  311. refreshListener.onRefresh();
  312. }
  313. }
  314. //下拉刷新的
  315. privatevoidmeasureView(Viewchild){
  316. //v这组布局参数宽度和高度
  317. ViewGroup.LayoutParamsp=child.getLayoutParams();
  318. if(p==null){
  319. //创建一个新组布局参数指定的宽度(填充)和高度(包裹)。
  320. p=newViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
  321. ViewGroup.LayoutParams.WRAP_CONTENT);
  322. }
  323. //d得到childWidthSpec(高度或宽度)的子视图
  324. intchildWidthSpec=ViewGroup.getChildMeasureSpec(0,0+0,p.width);
  325. intlpHeight=p.height;
  326. intchildHeightSpec;
  327. if(lpHeight>0){
  328. //创建一个测量规范基于所提供的大小和模式
  329. childHeightSpec=MeasureSpec.makeMeasureSpec(lpHeight,
  330. MeasureSpec.EXACTLY);
  331. }else{
  332. childHeightSpec=MeasureSpec.makeMeasureSpec(0,
  333. MeasureSpec.UNSPECIFIED);
  334. }
  335. //找出一个视图应该多大。父供应约束信息在宽度和高度参数
  336. child.measure(childWidthSpec,childHeightSpec);
  337. }
  338. publicvoidsetAdapter(BaseAdapteradapter){
  339. //设置最近刷新的时间
  340. SimpleDateFormatformat=newSimpleDateFormat("yyyy年MM月dd日HH:mm");
  341. Stringdate=format.format(newDate());
  342. lastUpdatedTextView.setText("最近更新:"+date);
  343. super.setAdapter(adapter);
  344. }
  345. }

12.接着是没有添加聊天功能的main3.java

  1. packagecom.wang;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.ImageView;
  5. publicclassmain3extendsActivity{
  6. protectedvoidonCreate(BundlesavedInstanceState){
  7. //TODOAuto-generatedmethodstub
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main3);
  10. }
  11. }
13.亲,由于用到Activity,要注意注册哦!!
  1. <activityandroid:name=".pullrefreshtest"></activity>
  2. <activityandroid:name=".QQonline"></activity>
  3. <activityandroid:name=".main3"></activity>

14.最后,还有一个重要的问题,本程序需要第三方jar包的支持,所以呢还需要导入第三方android-support-v4.jar包, 不会的参考 http://www.linuxidc.com/Linux/2012-08/67213.htm

下载地址

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/8月/3日/Android模仿QQ的左右滑动切换界面和下拉更新的效果/android-support-v4.jar包

15,这样过后就可以看到运行效果咯: 第一张是下拉刷新时候的界面,第二张是松开即可刷新时候的图片,第三张是刷新后的内容,第四张的QQ联系人的界面,第五张是还没有完善聊天消息的界面,第六张是左右滑动切换界面的实现效果


更多相关文章

  1. Android电源管理-休眠简要分析
  2. Android(安卓)- monkey 参数说明
  3. Android(安卓)Window 二 可移动悬浮窗口 WindowManager
  4. Android中简单的电话管理与短信管理App编写实例
  5. 读取SIM卡信息
  6. TextToSpeech 文本自动朗读
  7. android手机电池管理
  8. android实现接通和挂断电话
  9. Android消息推送(广播机制)+通知

随机推荐

  1. Android学习及如何利用android来赚钱
  2. 浅谈android中的反编译
  3. 谈Android终端厂商的如何建设 ——
  4. android之NDK(jni)开发笔记1——运行第一个
  5. 甩掉谷歌,Android阵营的集体性尴尬
  6. Android电话来电流程源码分析
  7. Android构建模块
  8. Android无线连接打印第三方开发的实现
  9. Android详情页结合ScrollView完成Title颜
  10. 年后面试,差点就痛失了字节跳动Android岗