到这里也布局算是添加完毕了,加载更多实现方式不同,我罗列两种
- 结合OnTouchListener
- 结合OnScrollListener
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下拉刷新上拉加载更多】
推荐阅读
- 自定义View6 -塔防小游戏:第三篇防御塔随意放置+多组野怪
- 自定义映射resultMap
- OpenDataV低代码平台增加自定义属性编辑
- vue3 自定义指令控制按钮权限
- 苹果手机怎么选择截屏(苹果手机怎么把截屏放在下拉菜单)
- 自定义铃声怎么删除不了 自定义铃声怎么删除
- lol自定义怎么无限金币 lol自定义怎么调无限火力
- OPPOreno6下拉菜单里的人像是什么
- excel如何设置下拉列表
- excel2007编辑自定义列表 excel2007下拉列表