自定义ListView下拉刷新上拉加载更多( 三 )

到这里也布局算是添加完毕了,加载更多实现方式不同,我罗列两种

  1. 结合OnTouchListener
  2. 结合OnScrollListener
方式一这里我先说方式一,因为我们下拉也是用的OnTouchListener,上拉加载也有几种状态,有加载中,还有暂无数据,普遍大家会写,已经到底了~,一直显示在最底部,提示用户没数据了,从而我们控件的上拉事件不可触发状态 。
private final int MORE_LOAD_STATE = 10;private final int MORE_LOADING = 11;private final int MORE_NO = 12;//已加载全部数据private int more_current_state = MORE_LOAD_STATE;//当前状态只有一点需要注意paddFooterY = paddFooterY > heightFooter ? 0 : paddFooterY;来判断不能滑出底部的高度 。
@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:downY = (int) event.getY();break;case MotionEvent.ACTION_MOVE:moveY = (int) event.getY();diffY = moveY - downY;//滑动的距离paddFooterY = heightFooter - diffY/3 ;//上拉加载更多if (getLastVisiblePosition() == getCount() - 1 && more_current_state != MORE_NO) {if( more_current_state!=MORE_LOADING){more_current_state = MORE_LOADING;if (iPullDownRefreshService != null) {iPullDownRefreshService.onLoadMore();}}paddFooterY = paddFooterY > heightFooter ? 0 : paddFooterY;viewFooter.setPadding(0,paddFooterY , 0, 0);return true;}break;case MotionEvent.ACTION_UP:break;}return false;}方式二public class MyListview extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener{@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {//没有滚动,或者已经用户触摸滚动动画结束if(scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_FLING){//最后一个可见if(getLastVisiblePosition() == getCount()-1){viewFooter.setPadding(0,0,0,0);if(iPullDownRefreshService!=null){iPullDownRefreshService.onLoadMore();}}}}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {}}注意:在init方法中添加
public void init(Context context) {setOnTouchListener(this);......this.setOnScrollListener(this);}完工
【自定义ListView下拉刷新上拉加载更多】

推荐阅读