ListView上拉加载更多的写法
listview或者recyclerview 一般我们会用到上拉加载,或者下拉加载
/** * Created by Arcturis lfy on 2019/12/12. * <p/> * 上拉加载更多的listview * <p>Copyright 2017 新疆西北星.</p> */ public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener { private Context mContext; private View mFootView; private int mTotalItemCount; private OnLoadMoreListener mLoadMoreListener; private boolean mIsLoading=false; public LoadMoreListView(Context context) { super(context); init(context); } public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } public LoadMoreListView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context){ this.mContext=context; mFootView= LayoutInflater.from(context).inflate(R.layout.header_view_layout,null); setOnScrollListener(this); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // 滑到顶部后自动加载,判断listview已经停止滚动并且最后可视的条目等于 0 //举一反三这里也可以判断是不是到底部 ,这样 index就是为 总数-1 int lastVisibleIndex = view.getLastVisiblePosition(); int firstIndex = view.getFirstVisiblePosition(); if (!mIsLoading && scrollState == OnScrollListener.SCROLL_STATE_IDLE // && lastVisibleIndex == mTotalItemCount-1 && firstIndex == 0 ) { mIsLoading=true; // addFooterView(mFootView); addHeaderView(mFootView); if (mLoadMoreListener!=null) { mLoadMoreListener.onloadMore(); } } } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mTotalItemCount=totalItemCount; if (firstVisibleItem == 0) { Toast.makeText(mContext, "滑到顶部",Toast.LENGTH_SHORT).show(); } if (visibleItemCount + firstVisibleItem == totalItemCount) { Toast.makeText(mContext, "滑到底部",Toast.LENGTH_SHORT).show(); } } public void setOnLoadMoreListener(OnLoadMoreListener listener){ mLoadMoreListener=listener; } public interface OnLoadMoreListener{ void onloadMore(); } public void setLoadCompleted(){ mIsLoading=false; // removeFooterView(mFootView); removeHeaderView(mFootView); } }
有一个Layout 就是Progress的在转
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:gravity="center" android:padding="15dp" android:layout_height="match_parent"> <ProgressBar style="@android:style/Widget.ProgressBar.Small" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="10dp" android:text="正在加载..." android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
那么我们怎么用呢
mListView.setAdapter(adapter); mListView.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() { @Override public void onloadMore() { loadMore(); } });
//添加到头部的写法 mData.add(0,object);即可 private void loadMore() { new Thread(){ @Override public void run() { super.run(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } mData.add(0,"111"); mData.add(0,"222"); mData.add(0,"333"); runOnUiThread(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); mListView.setLoadCompleted(); } }); } }.start(); }
这样我们就能实现上拉加载更多了!
下载加载更多未给出,大家自己改吧改吧就行了,看注释应该知道咋改了