0
点赞
收藏
分享

微信扫一扫

android上拉下拉加载更多数据



效果图

下拉刷新:

android上拉下拉加载更多数据_android

加载更多:

android上拉下拉加载更多数据_java_02


CustomListView.java


[java] ​​ view plain​​ ​​copy​​



  1. package
  2.   
  3. import
  4. import
  5. import
  6. import
  7. import
  8. import
  9. import
  10. import
  11. import
  12. import
  13. import
  14. import
  15. import
  16. import
  17. import
  18. import
  19. import
  20. import
  21. import
  22.   
  23. /**
  24.  * ListView下拉刷新
  25.  *
  26.  */
  27. public class CustomListView extends ListView implements
  28.   
  29. private final static int RELEASE_To_REFRESH = 0;  
  30. private final static int PULL_To_REFRESH = 1;  
  31. private final static int REFRESHING = 2;  
  32. private final static int DONE = 3;  
  33. private final static int LOADING = 4;  
  34.   
  35. // 实际的padding的距离与界面上偏移距离的比例
  36. private final static int RATIO = 3;  
  37.   
  38. private
  39.   
  40. private
  41.   
  42. private
  43. private
  44. private
  45. private
  46.   
  47.   
  48. private
  49. private
  50.   
  51. // 用于保证startY的值在一个完整的touch事件中只被记录一次
  52. private boolean
  53.   
  54. private int
  55. private int
  56.   
  57. private int
  58. private int
  59.   
  60. private int
  61.   
  62. private boolean
  63.   
  64. private
  65. private
  66.   
  67. private boolean
  68.       
  69. private
  70. private
  71. private
  72.   
  73. public
  74. super(context);  
  75.         init(context);  
  76.     }  
  77.   
  78. public
  79. super(context, attrs);  
  80.         init(context);  
  81.     }  
  82.   
  83. private void
  84.         setCacheColorHint(context.getResources().getColor(R.color.transparent));  
  85.         inflater = LayoutInflater.from(context);  
  86.   
  87. null);  
  88.   
  89.         arrowImageView = (ImageView) headView.findViewById(R.id.head_arrowImageView);  
  90. 70);  
  91. 50);  
  92.         progressBar = (ProgressBar) headView.findViewById(R.id.head_progressBar);  
  93.         tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);  
  94.         lastUpdatedTextView = (TextView) headView.findViewById(R.id.head_lastUpdatedTextView);  
  95.   
  96.         measureView(headView);  
  97.         headContentHeight = headView.getMeasuredHeight();  
  98.         headContentWidth = headView.getMeasuredWidth();  
  99.   
  100. 0, -1 * headContentHeight, 0, 0);  
  101.         headView.invalidate();  
  102.   
  103. "size", "width:" + headContentWidth + " height:"
  104.   
  105. null, false);  
  106. this);  
  107.   
  108. new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  109. new
  110. 250);  
  111. true);  
  112.   
  113. new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  114. new
  115. 200);  
  116. true);  
  117.   
  118.         state = DONE;  
  119. false;  
  120.           
  121. null);  
  122.         moreView.setVisibility(View.VISIBLE);  
  123.         moreProgressBar = (ProgressBar) moreView.findViewById(R.id.pull_to_refresh_progress);  
  124.         loadMoreView = (TextView) moreView.findViewById(R.id.load_more);  
  125. new
  126.               
  127. @Override
  128. public void
  129.                 onLoad();  
  130.             }  
  131.         });  
  132.         addFooterView(moreView);  
  133.     }  
  134.   
  135. public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2, int
  136.         firstItemIndex = firstVisiableItem;  
  137.     }  
  138.   
  139. public void onScrollStateChanged(AbsListView arg0, int
  140.           
  141.     }  
  142.   
  143. public boolean
  144.   
  145. if
  146. switch
  147. case
  148. if (firstItemIndex == 0
  149. true;  
  150. int) event.getY();  
  151.                 }  
  152. break;  
  153.   
  154. case
  155.   
  156. if
  157. if
  158.                           
  159.                     }  
  160. if
  161.                         state = DONE;  
  162.                         changeHeaderViewByState();  
  163.                     }  
  164. if
  165.                         state = REFRESHING;  
  166.                         changeHeaderViewByState();  
  167.                         onRefresh();  
  168.                     }  
  169.                 }  
  170.   
  171. false;  
  172. false;  
  173.   
  174. break;  
  175.   
  176. case
  177. int tempY = (int) event.getY();  
  178.   
  179. if (!isRecored && firstItemIndex == 0) {  
  180. true;  
  181.                     startY = tempY;  
  182.                 }  
  183.   
  184. if
  185.   
  186. // 保证在设置padding的过程中,当前的位置一直是在head,否则如果当列表超出屏幕的话,当在上推的时候,列表会同时进行滚动
  187. // 可以松手去刷新了
  188. if
  189.   
  190. 0);  
  191.   
  192. // 往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步
  193. if (((tempY - startY) / RATIO < headContentHeight) && (tempY - startY) > 0) {  
  194.                             state = PULL_To_REFRESH;  
  195.                             changeHeaderViewByState();  
  196.                         }  
  197. // 一下子推到顶了
  198. else if (tempY - startY <= 0) {  
  199.                             state = DONE;  
  200.                             changeHeaderViewByState();  
  201.                         }  
  202. // 往下拉了,或者还没有上推到屏幕顶部掩盖head的地步
  203.                     }  
  204. // 还没有到达显示松开刷新的时候,DONE或者是PULL_To_REFRESH状态
  205. if
  206.   
  207. 0);  
  208.   
  209. // 下拉到可以进入RELEASE_TO_REFRESH的状态
  210. if
  211.                             state = RELEASE_To_REFRESH;  
  212. true;  
  213.                             changeHeaderViewByState();  
  214.                         }  
  215. else if (tempY - startY <= 0) {  
  216.                             state = DONE;  
  217.                             changeHeaderViewByState();  
  218.                         }  
  219.                     }  
  220.   
  221. if
  222. if (tempY - startY > 0) {  
  223.                             state = PULL_To_REFRESH;  
  224.                             changeHeaderViewByState();  
  225.                         }  
  226.                     }  
  227.   
  228. if
  229. 0, -1 * headContentHeight + (tempY - startY) / RATIO, 0, 0);  
  230.   
  231.                     }  
  232.   
  233. if
  234. 0, (tempY - startY) / RATIO - headContentHeight, 0, 0);  
  235.                     }  
  236.   
  237.                 }  
  238.   
  239. break;  
  240.             }  
  241.         }  
  242.   
  243. return super.onTouchEvent(event);  
  244.     }  
  245.   
  246. // 当状态改变时候,调用该方法,以更新界面
  247. private void
  248. switch
  249. case
  250.             arrowImageView.setVisibility(View.VISIBLE);  
  251.             progressBar.setVisibility(View.GONE);  
  252.             tipsTextview.setVisibility(View.VISIBLE);  
  253.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  254.   
  255.             arrowImageView.clearAnimation();  
  256.             arrowImageView.startAnimation(animation);  
  257. "松开刷新");  
  258.   
  259. break;  
  260. case
  261.             progressBar.setVisibility(View.GONE);  
  262.             tipsTextview.setVisibility(View.VISIBLE);  
  263.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  264.             arrowImageView.clearAnimation();  
  265.             arrowImageView.setVisibility(View.VISIBLE);  
  266. // 是由RELEASE_To_REFRESH状态转变来的
  267. if
  268. false;  
  269.                 arrowImageView.clearAnimation();  
  270.                 arrowImageView.startAnimation(reverseAnimation);  
  271.   
  272. "下拉刷新");  
  273. else
  274. "下拉刷新");  
  275.             }  
  276. break;  
  277.   
  278. case
  279. 0, 0, 0, 0);  
  280.             progressBar.setVisibility(View.VISIBLE);  
  281.             arrowImageView.clearAnimation();  
  282.             arrowImageView.setVisibility(View.GONE);  
  283. "正在刷新...");  
  284.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  285.   
  286. break;  
  287. case
  288. 0, -1 * headContentHeight, 0, 0);  
  289.   
  290.             progressBar.setVisibility(View.GONE);  
  291.             arrowImageView.clearAnimation();  
  292.             arrowImageView.setImageResource(R.drawable.arrow);  
  293. "下拉刷新");  
  294.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  295.   
  296. break;  
  297.         }  
  298.     }  
  299.   
  300. public void
  301. this.refreshListener = refreshListener;  
  302. true;  
  303.     }  
  304.       
  305. public void
  306. this.loadListener = loadListener;  
  307.     }  
  308.   
  309. public interface
  310. public void
  311.     }  
  312.       
  313. public interface
  314. public void
  315.     }  
  316.   
  317. @SuppressWarnings("deprecation")  
  318. public void
  319.         state = DONE;  
  320. "最近更新:" + new
  321.         changeHeaderViewByState();  
  322.     }  
  323.       
  324. private void
  325. if (loadListener != null) {  
  326.             moreProgressBar.setVisibility(View.VISIBLE);  
  327.             loadMoreView.setText(getContext().getString(R.string.load_more));  
  328.             loadListener.onLoad();  
  329.         }  
  330.     }  
  331.       
  332. public void
  333. //      moreView.setVisibility(View.GONE);
  334.         moreProgressBar.setVisibility(View.GONE);  
  335.         loadMoreView.setText(getContext().getString(R.string.more_data));  
  336.     }  
  337.   
  338. private void
  339. if (refreshListener != null) {  
  340.             refreshListener.onRefresh();  
  341.         }  
  342.     }  
  343.   
  344. private void
  345.         ViewGroup.LayoutParams p = child.getLayoutParams();  
  346. if (p == null) {  
  347. new
  348.         }  
  349. int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);  
  350. int
  351. int
  352. if (lpHeight > 0) {  
  353.             childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);  
  354. else
  355. 0, MeasureSpec.UNSPECIFIED);  
  356.         }  
  357.         child.measure(childWidthSpec, childHeightSpec);  
  358.     }  
  359.   
  360. @SuppressWarnings("deprecation")  
  361. public void
  362. "最近更新:" + new
  363. super.setAdapter(adapter);  
  364.     }  
  365.   
  366. }  



在 CustomListView 中有2个回调接口,OnRefreshListener 和 OnLoadListener ,分别对应 下拉和点击加载更多 时候的回调函数。在下拉刷新完成之后要调用 mListView.onRefreshComplete(); 来隐藏掉 头部,调用 mListView.onLoadComplete(); 隐藏掉 底部的加载view。


header.xml


[html] ​​ view plain​​ ​​copy​​



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- ListView的头部 -->
  3.   
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content" >
  7.   
  8. <!-- 内容 -->
  9.   
  10. <RelativeLayout
  11. android:id="@+id/head_contentLayout"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:paddingLeft="30dp" >
  15.   
  16. <!-- 箭头图像、进度条 -->
  17.   
  18. <FrameLayout
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_alignParentLeft="true"
  22. android:layout_centerVertical="true" >
  23.   
  24. <!-- 箭头 -->
  25.   
  26. <ImageView
  27. android:id="@+id/head_arrowImageView"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="center"
  31. android:contentDescription="@string/app_name"
  32. android:src="@drawable/arrow" />
  33.   
  34. <!-- 进度条 -->
  35.   
  36. <ProgressBar
  37. android:id="@+id/head_progressBar"
  38. style="?android:attr/progressBarStyleSmall"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_gravity="center"
  42. android:indeterminateDrawable="@drawable/progressbar_bg"
  43. android:visibility="gone" />
  44. </FrameLayout>
  45.   
  46. <!-- 提示、最近更新 -->
  47.   
  48. <LinearLayout
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_centerHorizontal="true"
  52. android:gravity="center_horizontal"
  53. android:orientation="vertical" >
  54.   
  55. <!-- 提示 -->
  56.   
  57. <TextView
  58. android:id="@+id/head_tipsTextView"
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:text="@string/pull_to_refresh_pull_label"
  62. android:textColor="@color/pull_refresh_textview"
  63. android:textSize="20sp" />
  64.   
  65. <!-- 最近更新 -->
  66.   
  67. <TextView
  68. android:id="@+id/head_lastUpdatedTextView"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:text="@string/pull_to_refresh_refresh_lasttime"
  72. android:textColor="@color/gold"
  73. android:textSize="10sp" />
  74. </LinearLayout>
  75. </RelativeLayout>
  76.   
  77. </LinearLayout>





listfooter_more.xml


[html] ​​ view plain​​ ​​copy​​



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:gravity="center_horizontal"
  6. android:orientation="horizontal"
  7. android:padding="15dp"
  8. >
  9.   
  10. <ProgressBar
  11. android:id="@+id/pull_to_refresh_progress"
  12. style="@android:style/Widget.ProgressBar.Small.Inverse"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:gravity="center"
  16. android:indeterminate="true"
  17. android:visibility="gone" >
  18. </ProgressBar>
  19.   
  20. <TextView
  21. android:id="@+id/load_more"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginLeft="10.0dp"
  25. android:gravity="center"
  26. android:text="@string/more_data"
  27. android:textColor="@color/black" >
  28. </TextView>
  29.   
  30. </LinearLayout>




MainActivity.java


[java] ​​ view plain​​ ​​copy​​



  1. package
  2.   
  3. import
  4. import
  5.   
  6. import
  7. import
  8. import
  9. import
  10. import
  11. import
  12. import
  13. import
  14. import
  15. import
  16. import
  17. import
  18. import
  19. import
  20. import
  21.   
  22. import
  23. import
  24. import
  25. import
  26.   
  27. public class MainActivity extends
  28.       
  29. private static final String TAG = MainActivity.class.getSimpleName();  
  30.   
  31. private static final int LOAD_DATA_FINISH = 10;  
  32.   
  33. private static final int REFRESH_DATA_FINISH = 11;  
  34.       
  35. private List<AppInfo> mList = new
  36. private
  37. private
  38. private int count = 10;  
  39. private Handler handler = new
  40. public void
  41. switch
  42. case
  43.                   
  44. if(mAdapter!=null){  
  45.                     mAdapter.notifyDataSetChanged();  
  46.                 }  
  47. //下拉刷新完成
  48. break;  
  49. case
  50. if(mAdapter!=null){  
  51.                     mAdapter.notifyDataSetChanged();  
  52.                 }  
  53. //加载更多完成
  54. break;  
  55. default:  
  56. break;  
  57.             }  
  58.         };  
  59.     };  
  60.       
  61. @Override
  62. protected void
  63. super.onCreate(savedInstanceState);  
  64.         setContentView(R.layout.activity_main);  
  65.           
  66.         buildAppData();  
  67.           
  68. new CustomListAdapter(this);  
  69.         mListView = (CustomListView) findViewById(R.id.mListView);  
  70.         mListView.setAdapter(mAdapter);  
  71.           
  72. new
  73.               
  74. @Override
  75. public void
  76. //TODO 下拉刷新
  77. "onRefresh");  
  78. 0);  
  79.             }  
  80.         });  
  81.           
  82. new
  83.               
  84. @Override
  85. public void
  86. //TODO 加载更多
  87. "onLoad");  
  88. 1);  
  89.             }  
  90.         });  
  91.           
  92. new
  93.   
  94. @Override
  95. public void
  96. int position, long
  97.   
  98. "click position:"
  99.   
  100.             }  
  101.   
  102.         });  
  103.     }  
  104.       
  105. public void loadData(final int
  106. new
  107. @Override
  108. public void
  109.                   
  110. for(int i=count;i<count+10;i++){  
  111. new
  112.   
  113.                     ai.setAppIcon(BitmapFactory.decodeResource(getResources(),  
  114.                             R.drawable.ic_launcher));  
  115. "应用Demo_"
  116. "版本: " + (i % 10 + 1) + "." + (i % 8 + 2) + "."
  117. 6 + 3));  
  118. "大小: " + i * 10 + "MB");  
  119.   
  120.                     mList.add(ai);  
  121.                 }  
  122. 10;  
  123.                   
  124. try
  125. 300);  
  126. catch
  127.                     e.printStackTrace();  
  128.                 }  
  129.                   
  130. if(type==0){    //下拉刷新
  131. //                  Collections.reverse(mList); //逆序
  132.                     handler.sendEmptyMessage(REFRESH_DATA_FINISH);  
  133. else if(type==1){  
  134.                     handler.sendEmptyMessage(LOAD_DATA_FINISH);  
  135.                 }  
  136.                   
  137.             }  
  138.         }.start();  
  139.     }  
  140.       
  141. /**
  142.      * 初始化应用数据
  143.      */
  144. private void
  145. for (int i = 0; i < 10; i++) {  
  146. new
  147.   
  148.             ai.setAppIcon(BitmapFactory.decodeResource(getResources(),  
  149.                     R.drawable.ic_launcher));  
  150. "应用Demo_"
  151. "版本: " + (i % 10 + 1) + "." + (i % 8 + 2) + "."
  152. 6 + 3));  
  153. "大小: " + i * 10 + "MB");  
  154.   
  155.             mList.add(ai);  
  156.         }  
  157.     }  
  158.   
  159. @Override
  160. public boolean
  161. // Inflate the menu; this adds items to the action bar if it is present.
  162.         getMenuInflater().inflate(R.menu.main, menu);  
  163. return true;  
  164.     }  
  165.       
  166. public class CustomListAdapter extends
  167.   
  168. private
  169.   
  170. public
  171.             mInflater = LayoutInflater.from(context);  
  172.         }  
  173.   
  174. @Override
  175. public int
  176. return
  177.         }  
  178.   
  179. @Override
  180. public Object getItem(int
  181. return
  182.         }  
  183.   
  184. @Override
  185. public long getItemId(int
  186. return
  187.         }  
  188.   
  189. @Override
  190. public View getView(int
  191. if (getCount() == 0) {  
  192. return null;  
  193.             }  
  194.   
  195. null;  
  196. if (convertView == null) {  
  197. null);  
  198.   
  199. new
  200.                 holder.ivImage = (ImageView) convertView  
  201.                         .findViewById(R.id.ivIcon);  
  202.                 holder.tvName = (TextView) convertView  
  203.                         .findViewById(R.id.tvName);  
  204.                 holder.tvVer = (TextView) convertView.findViewById(R.id.tvVer);  
  205.                 holder.tvSize = (TextView) convertView  
  206.                         .findViewById(R.id.tvSize);  
  207.                 convertView.setTag(holder);  
  208. else
  209.                 holder = (ViewHolder) convertView.getTag();  
  210.             }  
  211.   
  212.             AppInfo ai = mList.get(position);  
  213.             holder.ivImage.setImageBitmap(ai.getAppIcon());  
  214.             holder.tvName.setText(ai.getAppName());  
  215.             holder.tvVer.setText(ai.getAppVer());  
  216.             holder.tvSize.setText(ai.getAppSize());  
  217.   
  218. return
  219.         }  
  220.     }  
  221.   
  222. public static class
  223. private
  224. private
  225. private
  226. private
  227.   
  228.     }  
  229. }  




list_item.xml


[html] ​​ view plain​​ ​​copy​​



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content" >
  6.   
  7. <ImageView
  8. android:id="@+id/ivIcon"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:contentDescription="@string/image_desc"
  12. android:src="@drawable/ic_launcher" />
  13.   
  14. <LinearLayout
  15. android:id="@+id/appInfo"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_marginLeft="5dip"
  19. android:layout_toRightOf="@id/ivIcon"
  20. android:orientation="vertical" >
  21.   
  22. <TextView
  23. android:id="@+id/tvName"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="@string/name"
  27. android:textColor="#000000"
  28. android:textSize="16sp" />
  29.   
  30. <TextView
  31. android:id="@+id/tvVer"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="@string/ver"
  35. android:textColor="#666666"
  36. android:textSize="13sp" />
  37.   
  38. <TextView
  39. android:id="@+id/tvSize"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="@string/size"
  43. android:textColor="#666666"
  44. android:textSize="13sp" />
  45. </LinearLayout>
  46.   
  47. <Button
  48. android:id="@+id/btnClick"
  49. android:layout_width="80dip"
  50. android:layout_height="wrap_content"
  51. android:layout_alignParentRight="true"
  52. android:layout_centerVertical="true"
  53. android:focusable="false"
  54. android:text="@string/mgr"
  55. android:textColor="#000000"
  56. android:textSize="16sp" />
  57.   
  58. </RelativeLayout>




举报

相关推荐

0 条评论