SwipeRefreshLayout只能实现下拉刷新,而不能实现上拉加载更多。所以这需要对其进行扩充。
1,首先继承SwipeRefreshLayout
,,,公共类SwipeRefreshAndMoreLoadLayout SwipeRefreshLayout延伸实现> package com.example.swiperefreshlayout; import android.content.Context; import android.support.v4.widget.SwipeRefreshLayout; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; public class  SwipeRefreshAndMoreLoadLayout extends SwipeRefreshLayout  implements OnScrollListener {/* * ,,,,*,滑动到最下面时的上拉操作 ,,,*/private int  mTounchslop;/* * ,,,,*,ListView的加载中页脚 ,,,*/private View  mListViewFooter; private ListView  mListView;/* * ,*按下时的y坐标 ,*/private int  mYdown; private int  mYlast; 时间=private boolean  isLoading 假; private OnLoadMoreListener  mOnLoadMoreListener; private TextView  mTvLoadMore; private int  mVisibleItemCount; private int  mTotalItemCount; public SwipeRefreshAndMoreLoadLayout (Context 上下文),{ 这(上下文,null); } public SwipeRefreshAndMoreLoadLayout (Context 上下文,AttributeSet attrs), { 超级(上下文,attrs); 时间=mTounchslop ViewConfiguration.get(上下文).getScaledTouchSlop (); 时间=mListViewFooter LayoutInflater.from(上下文).inflate (R.layout.footer_item, null); mTvLoadMore =, (TextView), mListViewFooter.findViewById (R.id.tv_loadmore); } @Override protected void  onLayout (boolean 改变,,int 左,int ,, int 对了, int 底部),{ super.onLayout(改变,,,,,,对,,底部);//初始化视图 if (mListView ==, null), { getListView (); } }/* * ,,,,*,获取ListView对象 ,,,*/private void  getListView (), { int childCount =, getChildCount (); if (childCount 祝辞,0),{ for (int 小姐:=,0;,小姐:& lt;, childCount;,我+ +),{ View child =, getChildAt(我); if (child instanceof 列表视图),{ mListView =,(视图)的孩子;//,设置滚动监听器给ListView,,使得滚动的情况下也可以自动加载 mListView.setOnScrollListener(这个); } } } } @Override public boolean  dispatchTouchEvent (MotionEvent ev), { int action =, ev.getAction (); switch (行动),{ case MotionEvent.ACTION_DOWN: mYdown =, (int) ev.getY (); 打破; case MotionEvent.ACTION_MOVE: mYlast =, (int) ev.getY (); 打破; case MotionEvent.ACTION_UP: if (canLoad ()), { loadData (); } 打破; 默认值: 打破; } return super.dispatchTouchEvent (ev); } public void  setAdapte (ListView listView, ListAdapter 适配器),{ if (listView !=, null), { listView.addFooterView (mListViewFooter); listView.setAdapter(适配器); listView.removeFooterView (mListViewFooter); } } private boolean  canLoad (), { return ! isLoading ,,, isPullup (),,,, isBottom (); } private boolean  enableBottomLoad (), { return ! isLoading ,,, isBottom (); } private boolean  isBottom (), { if (mListView !=, null ,,, mListView.getAdapter (), !=, null), { return mVisibleItemCount & lt;, mTotalItemCount ,,, mListView.getLastVisiblePosition (),==, mListView.getAdapter () .getCount () 1; } return 假; } private boolean  isPullup (), { return mYdown-mYlast 祝辞=,mTounchslop; } private void  loadData (), { if (mOnLoadMoreListener !=, null), { setLoading(真正的); mOnLoadMoreListener.onLoadMore (); } } public void  setLoading (boolean 加载),{ 时间=isLoading 加载; if (加载),{ mListView.addFooterView (mListViewFooter); },{else mListView.removeFooterView (mListViewFooter); 时间=mYdown 0; 时间=mYlast 0; } } public void  setLoadingContext (String 字符串),{ mTvLoadMore.setText(字符串); } public void  setLoadingContext (int 渣油),{ mTvLoadMore.setText(渣油); } @Override public void  onScrollStateChanged (AbsListView 看来,,int scrollState), { } @Override public void  onScroll (AbsListView 看来,,int firstVisibleItem, null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null