android listview 下拉刷新以及加载更多

本例是实现下拉时刷新并且底部设置加载更多选择并在下拉和回滑时加入相应的动画效果,

功能实现主要为自定义一个Layout在此布局中头部刷新主要是用到了在头部添加布局view控件并使用GestureDetector控制下拉时的变化以便控制滑动的效果,在自定义底部也加一个加载更多的布局,中间内嵌一个ListView控件以显示内容。具体步骤如下:

 

第一步:设计自定义布局PullDownListView继承FrameLayout 实现GestureDetector.OnGestureListenerAnimation.AnimationListener接口:

 

Java代码   收藏代码
  1. import android.content.Context;  
  2. import android.graphics.Rect;  
  3. import android.util.AttributeSet;  
  4. import android.util.Log;  
  5. import android.view.GestureDetector;  
  6. import android.view.Gravity;  
  7. import android.view.LayoutInflater;  
  8. import android.view.MotionEvent;  
  9. import android.view.View;  
  10. import android.view.ViewConfiguration;  
  11. import android.view.animation.Animation;  
  12. import android.view.animation.AnimationUtils;  
  13. import android.widget.AbsListView;  
  14. import android.widget.AbsListView.OnScrollListener;  
  15. import android.widget.FrameLayout;  
  16. import android.widget.ImageView;  
  17. import android.widget.LinearLayout;  
  18. import android.widget.ListView;  
  19. import android.widget.ProgressBar;  
  20. import android.widget.Scroller;  
  21. import android.widget.TextView;  
  22. import com.example.pulldownview.R;  
  23. public class PullDownListView extends FrameLayout implements  
  24. GestureDetector.OnGestureListener, Animation.AnimationListener {  
  25. public static int MAX_LENGHT = 0;  
  26. public static final int STATE_REFRESH = 1;  
  27. public static final int SCROLL_TO_CLOSE = 2;  
  28. public static final int SCROLL_TO_REFRESH = 3;  
  29. public static final double SCALE = 0.9d;  
  30. private static final int CLOSEDELAY = 300;  
  31. private static final int REFRESHDELAY = 300;  
  32. private Animation mAnimationDown;  
  33. private Animation mAnimationUp;  
  34. private ImageView mArrow;  
  35. private View emptyHeaderView;  
  36. private ProgressBar mProgressBar;  
  37. private TextView more;  
  38. private ProgressBar mProgressBar2;  
  39. private int mState;  
  40. private TextView mTitle;  
  41. public ListView mListView;  
  42. LinearLayout foot;  
  43. LinearLayout footer_layout;  
  44. LinearLayout header;  
  45. private GestureDetector mDetector;  
  46. private FlingRunnable mFlinger;  
  47. private int mPading;  
  48. private int mDestPading;  
  49. private int mLastTop;  
  50. private LinearLayout mFirstChild;  
  51. private FrameLayout mUpdateContent;  
  52. private OnRefreshListioner mRefreshListioner;  
  53. private boolean isAutoLoadMore = false;  
  54. private boolean hasMore = true;  
  55. private boolean isEnd = true;  
  56. private boolean listviewDoScroll = false;  
  57. private boolean isFirstLoading = false;  
  58. private boolean mLongPressing;// 如果设置为true说明刚好到了执行长按的时间  
  59. private boolean mPendingRemoved = false;//  
  60. private String pulldowntorefresh;  
  61. private String releasetorefresh;  
  62. private String loading;  
  63. Rect r = new Rect();  
  64. private MotionEvent downEvent;  
  65. private CheckForLongPress mPendingCheckForLongPress = new CheckForLongPress();  
  66. private CheckForLongPress2 mPendingCheckForLongPress2 = new CheckForLongPress2();  
  67. private float lastY;  
  68. private boolean useempty = true;  
  69. //这个标签作为测试用  
  70. String TAG = "PullDownListView";  
  71. /** 
  72.  * 长按检查方法执行1线程 
  73.  * @author Administrator 
  74.  * 
  75.  */  
  76. private class CheckForLongPress implements Runnable {  
  77. public void run() {  
  78. if (mListView.getOnItemLongClickListener() == null) {  
  79. else {  
  80. postDelayed(mPendingCheckForLongPress2, 100);  
  81. }  
  82. }  
  83. }  
  84. /** 
  85.  * 长按检查方法执行2线程 ----> 延后 100  
  86.  * @author Administrator 
  87.  * 
  88.  */  
  89. private class CheckForLongPress2 implements Runnable {  
  90. public void run() {  
  91. mLongPressing = true;  
  92. MotionEvent e = MotionEvent.obtain(downEvent.getDownTime(),downEvent.getEventTime()  
  93. + ViewConfiguration.getLongPressTimeout(),  
  94. MotionEvent.ACTION_CANCEL, downEvent.getX(),  
  95. downEvent.getY(), downEvent.getMetaState());  
  96. PullDownListView.super.dispatchTouchEvent(e);  
  97. }  
  98. }  
  99. class FlingRunnable implements Runnable {  
  100. private void startCommon() {  
  101. removeCallbacks(this);  
  102. }  
  103. public void run() {  
  104. boolean noFinish = mScroller.computeScrollOffset();  
  105. int curY = mScroller.getCurrY();  
  106. int deltaY = curY - mLastFlingY;  
  107. if (noFinish) {  
  108. move(deltaY, true);  
  109. mLastFlingY = curY;  
  110. post(this);  
  111. else {  
  112. removeCallbacks(this);  
  113. if (mState == SCROLL_TO_CLOSE) {  
  114. mState = -1;  
  115. }  
  116. }  
  117. }  
  118. public void startUsingDistance(int distance, int duration) {  
  119. if (distance == 0)  
  120. distance--;  
  121. startCommon();  
  122. mLastFlingY = 0;  
  123. mScroller.startScroll(000, distance, duration);  
  124. post(this);  
  125. }  
  126. private int mLastFlingY;  
  127. private Scroller mScroller;  
  128. public FlingRunnable() {  
  129. mScroller = new Scroller(getContext());  
  130. }  
  131. }  
  132. /** 
  133.  * 下拉刷新以及加载更多回调监听接口 
  134.  * @author Administrator 
  135.  * 
  136.  */  
  137. public interface OnRefreshListioner {  
  138. public abstract void onRefresh();  
  139. public abstract void onLoadMore();  
  140. }  
  141. /** 
  142.  * 直接new时调用的构造方法  
  143.  * @param context 
  144.  */  
  145. public PullDownListView(Context context) {  
  146. super(context);  
  147. mDetector = new GestureDetector(context, this);  
  148. mFlinger = new FlingRunnable();  
  149. init();  
  150. addRefreshBar();  
  151. }  
  152. /** 
  153.  * 在xml中使用时调用的构造方法 
  154.  * @param context 
  155.  */  
  156. public PullDownListView(Context context, AttributeSet att) {  
  157. super(context, att);  
  158. useempty = att.getAttributeBooleanValue(null"useempty"true);  
  159. mDetector = new GestureDetector(this);  
  160. mFlinger = new FlingRunnable();  
  161. init();  
  162. addRefreshBar();  
  163. }  
  164. View view;  
  165. /** 
  166.  * 添加刷新头部的控件 
  167.  */  
  168. private void addRefreshBar() {  
  169. //向上滑动的动画  
  170. mAnimationUp = AnimationUtils.loadAnimation(getContext(),R.anim.rotate_up);  
  171. mAnimationUp.setAnimationListener(this);  
  172. //向下滑动的动画  
  173. mAnimationDown = AnimationUtils.loadAnimation(getContext(),R.anim.rotate_down);  
  174. mAnimationDown.setAnimationListener(this);  
  175. //刷新头部的view  
  176. view = LayoutInflater.from(getContext()).inflate(R.layout.refresh_bar,null);  
  177. //添加view在本控件中  
  178. addView(view);  
  179. /* 
  180.  * 以下都是刷新头部的一些控件的设置 
  181.  */  
  182. mFirstChild = (LinearLayout) view;  
  183. mUpdateContent = (FrameLayout) getChildAt(0).findViewById(R.id.iv_content);  
  184. mArrow = new ImageView(getContext());  
  185. FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(  
  186. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  187. mArrow.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  188. mArrow.setLayoutParams(layoutparams);  
  189. mArrow.setImageResource(R.drawable.arrow_down);  
  190. mUpdateContent.addView(mArrow);  
  191. FrameLayout.LayoutParams layoutparams1 = new FrameLayout.LayoutParams(  
  192. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  193. layoutparams1.gravity = Gravity.CENTER;  
  194. mProgressBar = new ProgressBar(getContext(), null,  
  195. android.R.attr.progressBarStyleSmallInverse);  
  196. mProgressBar.setIndeterminate(false);  
  197. int i = getResources().getDimensionPixelSize(R.dimen.updatebar_padding);  
  198. mProgressBar.setPadding(i, i, i, i);  
  199. mProgressBar.setLayoutParams(layoutparams1);  
  200. mUpdateContent.addView(mProgressBar);  
  201. mTitle = (TextView) findViewById(R.id.tv_title);  
  202. }  
  203. public void setGone() {  
  204. mTitle.setVisibility(View.GONE);  
  205. mUpdateContent.setVisibility(View.GONE);  
  206. }  
  207. protected void onFinishInflate() {  
  208. super.onFinishInflate();  
  209. mListView = (ListView) getChildAt(1);  
  210. footer_layout = (LinearLayout) LayoutInflater.from(getContext())  
  211. .inflate(R.layout.empty_main, null);  
  212. foot = (LinearLayout) LayoutInflater.from(getContext()).inflate(  
  213. R.layout.ref2, null);  
  214. more = (TextView) foot.findViewById(R.id.ref);  
  215. mProgressBar2 = (ProgressBar) foot.findViewById(R.id.refbar);  
  216. mProgressBar2.setVisibility(View.GONE);  
  217. if (useempty) {  
  218. header = (LinearLayout) LayoutInflater.from(getContext()).inflate(  
  219. R.layout.empty_main, null);  
  220. mListView.addHeaderView(header);  
  221. }  
  222. mListView.addFooterView(footer_layout);  
  223. foot.setOnClickListener(new View.OnClickListener() {  
  224. public void onClick(View v) {  
  225. if (!isAutoLoadMore)  
  226. onLoadMore();  
  227. }  
  228. });  
  229. mListView.setOnScrollListener(new OnScrollListener() {  
  230. public void onScrollStateChanged(AbsListView view, int scrollState) {  
  231. if (isEnd && scrollState == SCROLL_STATE_IDLE && hasMore  
  232. && isAutoLoadMore) {  
  233. onLoadMore();  
  234. }  
  235. }  
  236. public void onScroll(AbsListView view, int f, int v, int t) {  
  237. if (isAutoLoadMore) {  
  238. if (f + v >= t - 1)  
  239. isEnd = true;  
  240. else  
  241. isEnd = false;  
  242. }  
  243. }  
  244. });  
  245. }  
  246. /** 
  247.  * 设置没有数据时默认图片 
  248.  *  
  249.  * @param empty 
  250.  */  
  251. public void setEmptyHeaderView(View empty) {  
  252. emptyHeaderView = empty;  
  253. }  
  254. /** 
  255.  * 添加空的定部view 
  256.  */  
  257. public void addEmptyHeaderView() {  
  258. header.removeAllViews();  
  259. if (emptyHeaderView != null)  
  260. header.addView(emptyHeaderView);  
  261. }  
  262. /** 
  263.  * 移除顶部空view 
  264.  */  
  265. public void removeEmptyHeaderView() {  
  266. if (emptyHeaderView != null)  
  267. header.removeView(emptyHeaderView);  
  268. }  
  269. /** 
  270.  * 初始化设置及变量 
  271.  */  
  272. private void init() {  
  273. MAX_LENGHT = getResources().getDimensionPixelSize(  
  274. R.dimen.updatebar_height);// 62.0dip  
  275. setDrawingCacheEnabled(false);  
  276. setBackgroundDrawable(null);  
  277. setClipChildren(false);  
  278. mDetector.setIsLongpressEnabled(false);  
  279. mPading = -MAX_LENGHT;  
  280. mLastTop = -MAX_LENGHT;  
  281. pulldowntorefresh = getContext().getText(R.string.drop_dowm).toString();  
  282. releasetorefresh = getContext().getText(R.string.release_update)  
  283. .toString();  
  284. loading = getContext().getText(R.string.loading).toString();  
  285. }  
  286. /** deltaY > 0 向上 */  
  287. private boolean move(float deltaY, boolean auto) {  
  288. //move 方法执行 "   
  289. if (deltaY > 0 && mFirstChild.getTop() == -MAX_LENGHT) {  
  290. mPading = -MAX_LENGHT;  
  291. return false;  
  292. }  
  293. if (auto) {  
  294. //move 方法执行  
  295. if (mFirstChild.getTop() - deltaY < mDestPading) {  
  296. deltaY = mFirstChild.getTop() - mDestPading;  
  297. }  
  298. mFirstChild.offsetTopAndBottom((int) -deltaY);  
  299. mListView.offsetTopAndBottom((int) -deltaY);  
  300. mPading = mFirstChild.getTop();  
  301. if (mDestPading == 0 && mFirstChild.getTop() == 0  
  302. && mState == SCROLL_TO_REFRESH) {  
  303. //onRefresh 刷新方法执行  
  304. onRefresh();  
  305. else if (mDestPading == -MAX_LENGHT) {  
  306. }  
  307. invalidate();  
  308. updateView();  
  309. return true;  
  310. else {  
  311. if (mState != STATE_REFRESH  
  312. || (mState == STATE_REFRESH && deltaY > 0)) {  
  313. mFirstChild.offsetTopAndBottom((int) -deltaY);  
  314. mListView.offsetTopAndBottom((int) -deltaY);  
  315. mPading = mFirstChild.getTop();  
  316. else if (mState == STATE_REFRESH && deltaY < 0  
  317. && mFirstChild.getTop() <= 0) {  
  318. if (mFirstChild.getTop() - deltaY > 0) {  
  319. deltaY = mFirstChild.getTop();  
  320. }  
  321. mFirstChild.offsetTopAndBottom((int) -deltaY);  
  322. mListView.offsetTopAndBottom((int) -deltaY);  
  323. mPading = mFirstChild.getTop();  
  324. }  
  325. }  
  326. if (deltaY > 0 && mFirstChild.getTop() <= -MAX_LENGHT) {  
  327. mPading = -MAX_LENGHT;  
  328. deltaY = -MAX_LENGHT - mFirstChild.getTop();  
  329. mFirstChild.offsetTopAndBottom((int) deltaY);  
  330. mListView.offsetTopAndBottom((int) deltaY);  
  331. mPading = mFirstChild.getTop();  
  332. updateView();  
  333. invalidate();  
  334. return false;  
  335. }  
  336. updateView();  
  337. invalidate();  
  338. return true;  
  339. }  
  340. private void updateView() {  
  341. String s = "";  
  342. if (mState != STATE_REFRESH) {  
  343. if (mFirstChild.getTop() < 0) {  
  344. mArrow.setVisibility(View.VISIBLE);  
  345. mProgressBar.setVisibility(View.INVISIBLE);  
  346. mTitle.setText(pulldowntorefresh);  
  347. if (mLastTop >= 0 && mState != SCROLL_TO_CLOSE) {  
  348. mArrow.startAnimation(mAnimationUp);//向上移动动画  
  349. }  
  350. else if (mFirstChild.getTop() > 0) {  
  351. mTitle.setText(releasetorefresh + s);  
  352. mProgressBar.setVisibility(View.INVISIBLE);  
  353. mArrow.setVisibility(View.VISIBLE);  
  354. if (mLastTop <= 0) {  
  355. mArrow.startAnimation(mAnimationDown);//向下移动动画  
  356. }  
  357. }  
  358. }  
  359. mLastTop = mFirstChild.getTop();  
  360. }  
  361. //release 方法执行   
  362. private boolean release() {  
  363. if (listviewDoScroll) {  
  364. listviewDoScroll = false;  
  365. return true;  
  366. }  
  367. if (mFirstChild.getTop() > 0) {  
  368. scrollToUpdate(false);  
  369. else {  
  370. scrollToClose();  
  371. }  
  372. invalidate();  
  373. return false;  
  374. }  
  375. private void scrollToClose() {  
  376. mDestPading = -MAX_LENGHT;  
  377. mFlinger.startUsingDistance(MAX_LENGHT, CLOSEDELAY);  
  378. }  
  379. //scrollToUpdate 方法执行  
  380. public void scrollToUpdate(boolean load) {  
  381. mState = SCROLL_TO_REFRESH;  
  382. mDestPading = 0;  
  383. if (load) {  
  384. mFlinger.startUsingDistance(50, REFRESHDELAY);  
  385. load = false;  
  386. else  
  387. mFlinger.startUsingDistance(mFirstChild.getTop(), REFRESHDELAY);  
  388. }  
  389. private void onRefresh() {  
  390. mState = STATE_REFRESH;  
  391. mTitle.setText(loading);  
  392. mProgressBar.setVisibility(View.VISIBLE);  
  393. mArrow.setVisibility(View.INVISIBLE);  
  394. if (mRefreshListioner != null) {  
  395. mRefreshListioner.onRefresh();  
  396. }  
  397. }  
  398. public void onRefreshComplete() {  
  399. onRefreshComplete(null);  
  400. }  
  401. public void onRefreshComplete(String date) {  
  402. mState = SCROLL_TO_CLOSE;  
  403. mArrow.setImageResource(R.drawable.arrow_down);  
  404. mProgressBar2.setVisibility(View.INVISIBLE);  
  405. updateCommon();  
  406. scrollToClose();  
  407. }  
  408. public void setMore(boolean hasMore) {  
  409. if (hasMore) {  
  410. mListView.setFooterDividersEnabled(true);  
  411. footer_layout.removeAllViews();  
  412. footer_layout.addView(foot);  
  413. else {  
  414. mListView.setFooterDividersEnabled(false);  
  415. footer_layout.removeAllViews();  
  416. }  
  417. }  
  418. private void updateCommon() {  
  419. if (mListView.getCount() == (mListView.getHeaderViewsCount() + mListView  
  420. .getFooterViewsCount())) {  
  421. Log.e("out""数据为空");  
  422. if (useempty)  
  423. addEmptyHeaderView();  
  424. else {  
  425. removeEmptyHeaderView();  
  426. mListView.setFooterDividersEnabled(false);  
  427. footer_layout.removeAllViews();  
  428. }  
  429. }  
  430. public void setFoot() {  
  431. footer_layout.setVisibility(View.GONE);  
  432. }  
  433. public void onFirstLoad() {  
  434. if (footer_layout.getChildCount() == 0) {  
  435. footer_layout.addView(foot);  
  436. }  
  437. isFirstLoading = true;  
  438. foot.setEnabled(false);  
  439. //onFirstLoad 方法执行   
  440. mState = STATE_REFRESH;  
  441. mProgressBar2.setVisibility(View.VISIBLE);  
  442. more.setText(R.string.loading);  
  443. }  
  444. public void onLoadMore() {  
  445. //onLoadMore 方法执行   
  446. foot.setEnabled(false);  
  447. mState = STATE_REFRESH;  
  448. mProgressBar2.setVisibility(View.VISIBLE);  
  449. more.setText(R.string.loading);  
  450. if (mRefreshListioner != null) {  
  451. mRefreshListioner.onLoadMore();  
  452. }  
  453. }  
  454. public void onLoadMoreComplete(String date) {  
  455. mState = -1;  
  456. mProgressBar2.setVisibility(View.INVISIBLE);  
  457. more.setText(R.string.seen_more);  
  458. updateCommon();  
  459. if (isFirstLoading)  
  460. isFirstLoading = false;  
  461. foot.setEnabled(true);  
  462. }  
  463. public void onLoadMoreComplete() {  
  464. onLoadMoreComplete(null);  
  465. }  
  466. public boolean dispatchTouchEvent(MotionEvent e) {  
  467. if (isFirstLoading) {  
  468. return false;  
  469. }  
  470. int action;  
  471. float y = e.getY();  
  472. action = e.getAction();  
  473. if (mLongPressing && action != MotionEvent.ACTION_DOWN) {  
  474. return false;  
  475. }  
  476. if (e.getAction() == MotionEvent.ACTION_DOWN) {  
  477. mLongPressing = true;  
  478. }  
  479. boolean handled = true;  
  480. handled = mDetector.onTouchEvent(e);  
  481. switch (action) {  
  482. case MotionEvent.ACTION_UP:  
  483. boolean f1 = mListView.getTop() <= e.getY()  
  484. && e.getY() <= mListView.getBottom();  
  485. if (!handled && mFirstChild.getTop() == -MAX_LENGHT && f1  
  486. || mState == STATE_REFRESH) {  
  487. super.dispatchTouchEvent(e);  
  488. else {  
  489. //执行释放方法   
  490. handled = release();  
  491. }  
  492. break;  
  493. case MotionEvent.ACTION_CANCEL:  
  494. handled = release();  
  495. super.dispatchTouchEvent(e);  
  496. break;  
  497. case MotionEvent.ACTION_DOWN:  
  498. downEvent = e;  
  499. mLongPressing = false;  
  500. //长按的时间间隔  
  501. postDelayed(mPendingCheckForLongPress,  
  502. ViewConfiguration.getLongPressTimeout() + 100);  
  503. mPendingRemoved = false;  
  504. super.dispatchTouchEvent(e);  
  505. break;  
  506. case MotionEvent.ACTION_MOVE:  
  507. float deltaY = lastY - y;  
  508. lastY = y;  
  509. if (!mPendingRemoved) {  
  510. removeCallbacks(mPendingCheckForLongPress);  
  511. mPendingRemoved = true;  
  512. }  
  513. if (!handled && mFirstChild.getTop() == -MAX_LENGHT) {  
  514. try {  
  515. return super.dispatchTouchEvent(e);  
  516. catch (Exception e2) {  
  517. e2.printStackTrace();  
  518. return true;  
  519. }  
  520. else if (handled && mListView.getTop() > 0 && deltaY < 0) {// deltaY小于0,向�?  
  521. e.setAction(MotionEvent.ACTION_CANCEL);  
  522. super.dispatchTouchEvent(e);  
  523. }  
  524. break;  
  525. default:  
  526. break;  
  527. }  
  528. return true;  
  529. }  
  530. public void onAnimationEnd(Animation animation) {  
  531. int top = mFirstChild.getTop();  
  532. if (top < 0)  
  533. mArrow.setImageResource(R.drawable.arrow_down);  
  534. else if (top > 0)  
  535. mArrow.setImageResource(R.drawable.arrow_up);  
  536. else {  
  537. if (top < mLastTop) {  
  538. mArrow.setImageResource(R.drawable.arrow_down);  
  539. else {  
  540. mArrow.setImageResource(R.drawable.arrow_up);  
  541. }  
  542. }  
  543. }  
  544. public void onAnimationRepeat(Animation animation) {  
  545. }  
  546. public void onAnimationStart(Animation animation) {  
  547. }  
  548. public boolean onDown(MotionEvent e) {  
  549. return false;  
  550. }  
  551. public boolean onFling(MotionEvent motionevent, MotionEvent e, float f,  
  552. float f1) {  
  553. return false;  
  554. }  
  555. protected void onLayout(boolean flag, int i, int j, int k, int l) {  
  556. int top = mPading;  
  557. int w = getMeasuredWidth();  
  558. mFirstChild.layout(0, top, w, top + MAX_LENGHT);  
  559. int h = getMeasuredHeight() + mPading + MAX_LENGHT;  
  560. mListView.layout(0, top + MAX_LENGHT, w, h);  
  561. }  
  562. public void onLongPress(MotionEvent e) {  
  563. }  
  564. /** deltaY > 0 向上 */  
  565. public boolean onScroll(MotionEvent curdown, MotionEvent cur, float deltaX,  
  566. float deltaY) {  
  567. deltaY = (float) ((double) deltaY * SCALE);  
  568. boolean handled = false;  
  569. boolean flag = false;  
  570. if (mListView.getCount() == 0) {  
  571. flag = true;  
  572. else {  
  573. View c = mListView.getChildAt(0);  
  574. if (mListView.getFirstVisiblePosition() == 0 && c != null  
  575. && c.getTop() == 0) {  
  576. flag = true;  
  577. }  
  578. }  
  579. if (deltaY < 0F && flag || getChildAt(0).getTop() > -MAX_LENGHT) { // deltaY  
  580. // <  
  581. // 0  
  582. // 向下  
  583. handled = move(deltaY, false);  
  584. else  
  585. handled = false;  
  586. return handled;  
  587. }  
  588. public void onShowPress(MotionEvent motionevent) {  
  589. }  
  590. public boolean onSingleTapUp(MotionEvent motionevent) {  
  591. return false;  
  592. }  
  593. public void setRefreshListioner(OnRefreshListioner RefreshListioner) {  
  594. mRefreshListioner = RefreshListioner;  
  595. }  
  596. public boolean isAutoLoadMore() {  
  597. return isAutoLoadMore;  
  598. }  
  599. public void setAutoLoadMore(boolean isAutoLoadMore) {  
  600. this.isAutoLoadMore = isAutoLoadMore;  
  601. if (!isAutoLoadMore) {  
  602. foot.setOnClickListener(new View.OnClickListener() {  
  603. public void onClick(View v) {  
  604. onLoadMore();  
  605. }  
  606. });  
  607. mListView.setOnScrollListener(null);  
  608. else {  
  609. mListView.setOnScrollListener(new OnScrollListener() {  
  610. public void onScrollStateChanged(AbsListView view,  
  611. int scrollState) {  
  612. if (isEnd && scrollState == SCROLL_STATE_IDLE && hasMore) {  
  613. onLoadMore();  
  614. }  
  615. }  
  616. public void onScroll(AbsListView view, int f, int v, int t) {  
  617. if (f + v >= t - 1)  
  618. isEnd = true;  
  619. else  
  620. isEnd = false;  
  621. }  
  622. });  
  623. foot.setOnClickListener(null);  
  624. }  
  625. }  
  626. public void setHasMore(boolean hasMore) {  
  627. this.hasMore = hasMore;  
  628. }  
  629. public void removeFoot() {  
  630. footer_layout.removeAllViews();  
  631. }  
  632. public void addFoot() {  
  633. footer_layout.removeAllViews();  
  634. footer_layout.addView(foot);  
  635. }  
  636. }  
 

 

第二步:设计xml这里注意的是在自定义控件PullDownListView中内嵌一个ListView控件

 

 

Xml代码   收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.        <com.example.widget.PullDownListView  
  6.         android:id="@+id/sreach_list"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent" >  
  9.         <ListView  
  10.             android:id="@+id/list"  
  11.             android:layout_width="fill_parent"  
  12.             android:layout_height="fill_parent"  
  13.             android:layout_weight="0.0"  
  14.             android:divider="@null"  
  15.             android:drawSelectorOnTop="false"  
  16.             android:fadingEdgeLength="0.0sp" />  
  17.     </com.example.widget.PullDownListView>  
  18. </RelativeLayout>  
 

 

 

第三步:编写MainActivity 实现PullDownListView.OnRefreshListioner回调接口实现功能的演示:

 

Java代码   收藏代码
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.widget.ListView;  
  7. import com.example.widget.PullDownListView;  
  8. public class MainActivity extends Activity  implements PullDownListView.OnRefreshListioner{  
  9. private PullDownListView mPullDownView;  
  10. private ListView mListView;  
  11. private List<String> list = new ArrayList<String>();  
  12. private MyAdapter adapter;  
  13. private Handler mHandler = new Handler();  
  14. private int maxAount = 20;//设置了最大数据值  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         mPullDownView = (PullDownListView) findViewById(R.id.sreach_list);  
  20. mPullDownView.setRefreshListioner(this);  
  21. mListView = mPullDownView.mListView;  
  22. addLists(10);  
  23.     adapter = new MyAdapter(this,list);  
  24.     mPullDownView.setMore(true);//这里设置true表示还有更多加载,设置为false底部将不显示更多  
  25.     mListView.setAdapter(adapter);  
  26.     }  
  27.       
  28.     private void addLists(int n){  
  29.       
  30.          n += list.size();  
  31.          for(int i=list.size();i<n;i++){  
  32.         list.add("选项"+i);  
  33.      }  
  34.     }  
  35.       
  36.     /** 
  37.      * 刷新,先清空list中数据然后重新加载更新内容 
  38.      */  
  39. public void onRefresh() {  
  40. mHandler.postDelayed(new Runnable() {  
  41. public void run() {  
  42. list.clear();  
  43. addLists(10);  
  44. mPullDownView.onRefreshComplete();//这里表示刷新处理完成后把上面的加载刷新界面隐藏  
  45. mPullDownView.setMore(true);//这里设置true表示还有更多加载,设置为false底部将不显示更多  
  46. adapter.notifyDataSetChanged();  
  47. }  
  48. }, 1500);  
  49. }  
  50. /** 
  51.  * 加载更多,在原来基础上在添加新内容 
  52.  */  
  53. public void onLoadMore() {  
  54. mHandler.postDelayed(new Runnable() {  
  55. public void run() {  
  56. addLists(5);//每次加载五项新内容  
  57. mPullDownView.onLoadMoreComplete();//这里表示加载更多处理完成后把下面的加载更多界面(隐藏或者设置字样更多)  
  58. if(list.size()<maxAount)//判断当前list中已添加的数据是否小于最大值maxAount,是那么久显示更多否则不显示  
  59. mPullDownView.setMore(true);//这里设置true表示还有更多加载,设置为false底部将不显示更多  
  60. else  
  61. mPullDownView.setMore(false);  
  62. adapter.notifyDataSetChanged();  
  63. }  
  64. }, 1500);  
  65. }  
  66.       
  67. }  
 

 

第四:运行效果如图:




 

 

 



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值