自定义listView的上啦加载 带回弹

来源:互联网 发布:通联数据app 编辑:程序博客网 时间:2024/05/22 00:07

先看下效果


listview的代码

import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.animation.DecelerateInterpolator;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.ListView;import android.widget.Scroller;import android.widget.TextView;public class MyListView extends ListView implements OnScrollListener {private boolean isLoadingMore = false;// 是否 滑动到最后一行private boolean isLoad = false;// 是否已经是在加载中了public static final int state_down_default = 0;// 上啦的 默认状态public static final int state_down_loosen = 1;// 上啦的 松开加载public static final int state_down_load = 2;// 上啦的 加载中...private int loadState = state_down_default;private MyListViewListener loadListner;// 上啦加载的监听private View footerView;// 加载更多布局private TextView fTv;// 显示加载 更多提示private int fHeight;// 底部的高度private int startY = -1;// 开始的 y坐标private Scroller mScroller; // 用来处理回弹的private int htTime = 200;// 回弹的总时间 这个决定回弹的速度public MyListView(Context context) {this(context, null, 0);}public MyListView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);addLoadXML();}public void addLoadXML() {footerView = View.inflate(getContext(), R.layout.lv_footerview, null);this.addFooterView(footerView);fTv = (TextView) footerView.findViewById(R.id.tv_lv_footerview);fTv.setText("加载更多");footerView.measure(0, 0);fHeight = footerView.getMeasuredHeight();footerView.setPadding(0, -fHeight, 0, 0);// 设置隐藏this.setOnScrollListener(this);mScroller = new Scroller(getContext(), new DecelerateInterpolator());}@Overridepublic boolean onTouchEvent(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:// 按下startY = -1;break;case MotionEvent.ACTION_MOVE:// 移动if (isLoadingMore) {// 只有 滚动到最后一行才会 加载if (!isLoad) {// 只有没有加载 才能加载if (this.getLastVisiblePosition() == getCount() - 1) {// 并且显示到最后一条if (startY == -1) {// 把开始位置放到这里 是因为只有当滑动到最后一行 才开始显示// footerViewstartY = (int) ev.getRawY();}int endY = (int) ev.getRawY();int des = Math.abs(endY - startY);int padding = des - fHeight;footerView.setPadding(0, 0, 0, padding);// 设置显示的高度设置这个就会移动if (padding > 40 && loadState == state_down_default) {// 默认状态// 并且大于40// 就设置成松开加载loadState = state_down_loosen;getRefreshDownstate();} else if (padding <= 40&& loadState != state_down_default) {// 不是默认状态// 并且滑动的少与40// 就还原成默认的loadState = state_down_default;getRefreshDownstate();}}}}break;case MotionEvent.ACTION_UP:// 离开startY = -1;if (isLoadingMore) {if (loadState == state_down_loosen) {startScroll();// 设置回弹} else if (loadState == state_down_default) {// 如果是默认状态// 直接隐藏footerViewfooterView.setPadding(0, -fHeight, 0, 0);}}break;}return super.onTouchEvent(ev);}@Overridepublic void computeScroll() {if (mScroller.computeScrollOffset()) {// 这里就是每 小一次回弹 都要取设置 位置footerView.setPadding(0, 0, 0, mScroller.getCurrY());postInvalidate();// 更新}}/** * 刷新底部 */private void getRefreshDownstate() {switch (loadState) {case state_down_default:// 默认状态fTv.setText("上啦加载更多");isLoad = false;break;case state_down_loosen:// 松开加载fTv.setText("松开加载");isLoad = false;break;case state_down_load:// 加载中fTv.setText("加载中...");isLoad = true;break;}}@Overridepublic void onScrollStateChanged(AbsListView arg0, int scrollState) {// 判断是否在滑动}@Overridepublic void onScroll(AbsListView arg0, int firstVisibleItem,int visibleItemCount, int totalItemCount) {// 判断是 上滑 还是下滑// 判断是否滚到最后一行if (firstVisibleItem + visibleItemCount == totalItemCount&& totalItemCount > 0) {isLoadingMore = true;} else {isLoadingMore = false;}}/** * 回弹的动画 */public void startScroll() {int padding = footerView.getPaddingBottom();if (padding > 0) {// 正在加载中 并且回弹loadState = state_down_load;getRefreshDownstate();mScroller.startScroll(0, padding, 0, -padding, htTime);// 这里只要把所有的// padding// 回弹完,就是刚好显示正在加载invalidate();if (loadListner != null) {loadListner.onLoadMore();}}}/** * 停止 加载 */public void stopLoadMore() {loadState = state_down_default;getRefreshDownstate();footerView.setPadding(0, 0, 0, -fHeight);}/** * 设置 上啦监听 */public void setLoadListner(MyListViewListener loadListner) {this.loadListner = loadListner;}public interface MyListViewListener {public void onLoadMore();}}

里面注释很详细,底部布局可以自己去修改



源码下载




阅读全文
0 0