SwipeRefreshLayout只能实现下拉刷新,而不能实现上拉加载更多。所以这需要对其进行扩充。
1、首先继承SwipeRefreshLayout
public class SwipeRefreshAndMoreLoadLayout extends SwipeRefreshLayout implements OnScrollListener {
接下来直接替代码了
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的加载中footer */ private View mListViewFooter; private ListView mListView; /** * 按下时的y坐标 */ private int mYdown; private int mYlast; private boolean isLoading = false; private OnLoadMoreListener mOnLoadMoreListener; private TextView mTvLoadMore; private int mVisibleItemCount; private int mTotalItemCount; public SwipeRefreshAndMoreLoadLayout(Context context) { this(context, null); } public SwipeRefreshAndMoreLoadLayout(Context context, AttributeSet attrs) { super(context, attrs); mTounchslop = ViewConfiguration.get(context).getScaledTouchSlop(); mListViewFooter = LayoutInflater.from(context).inflate(R.layout.footer_item, null); mTvLoadMore = (TextView) mListViewFooter.findViewById(R.id.tv_loadmore); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); //初始化ListView if (mListView == null) { getListView(); } } /** * 获取ListView对象 */ private void getListView() { int childCount = getChildCount(); if (childCount > 0) { for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child instanceof ListView) { mListView = (ListView)child; // 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载 mListView.setOnScrollListener(this); } } } } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mYdown = (int)ev.getY(); break; case MotionEvent.ACTION_MOVE: mYlast = (int)ev.getY(); break; case MotionEvent.ACTION_UP: if (canLoad()) { loadData(); } break; default: break; } return super.dispatchTouchEvent(ev); } public void setAdapte(ListView listView, ListAdapter adapter) { if (listView != null) { listView.addFooterView(mListViewFooter); listView.setAdapter(adapter); 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 < mTotalItemCount && mListView.getLastVisiblePosition() == mListView.getAdapter().getCount()-1; } return false; } private boolean isPullup() { return mYdown-mYlast >= mTounchslop; } private void loadData() { if (mOnLoadMoreListener != null) { setLoading(true); mOnLoadMoreListener.onLoadMore(); } } public void setLoading(boolean loading) { isLoading = loading; if (loading) { mListView.addFooterView(mListViewFooter); } else { mListView.removeFooterView(mListViewFooter); mYdown = 0; mYlast = 0; } } public void setLoadingContext(String string) { mTvLoadMore.setText(string); } public void setLoadingContext(int resId) { mTvLoadMore.setText(resId); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mVisibleItemCount = visibleItemCount; mTotalItemCount = totalItemCount; if (visibleItemCount < totalItemCount && enableBottomLoad()) { loadData(); } } public void setOnLoadMoreListener(OnLoadMoreListener listener) { mOnLoadMoreListener = listener; } public static interface OnLoadMoreListener { void onLoadMore(); } }
package com.example.swiperefreshlayout; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import com.example.swiperefreshlayout.SwipeRefreshAndMoreLoadLayout.OnLoadMoreListener; @SuppressLint("InlinedApi") public class MainActivity extends Activity implements OnRefreshListener, OnLoadMoreListener { private SwipeRefreshAndMoreLoadLayout mRefreshLayout; private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.lv_listview); mRefreshLayout = (SwipeRefreshAndMoreLoadLayout) findViewById(R.id.refresh); mRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); mRefreshLayout.setOnRefreshListener(this); mRefreshLayout.setOnLoadMoreListener(this); mRefreshLayout.setAdapte(mListView, new MyAdapter()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { mRefreshLayout.setRefreshing(false); } }, 5000); } @Override public void onLoadMore() { mRefreshLayout.setLoadingContext("正在加载"); new Handler().postDelayed(new Runnable() { @Override public void run() { mRefreshLayout.setLoading(false); } }, 5000); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return 30; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View inflate = null; TextView tvItem = null; if (convertView == null) { inflate = getLayoutInflater().inflate(R.layout.listview_item, null); tvItem = (TextView) inflate.findViewById(R.id.tv_item); inflate.setTag(tvItem); } else { inflate = convertView; tvItem = (TextView) inflate.getTag(); } tvItem.setText("下拉刷新item"+position); return inflate; } } }
<com.example.swiperefreshlayout.SwipeRefreshAndMoreLoadLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/refresh" tools:context=".MainActivity" > <ListView android:id="@+id/lv_listview" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </com.example.swiperefreshlayout.SwipeRefreshAndMoreLoadLayout>
源代码链接:http://download.csdn.net/detail/dennisruan/9433709
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。