SwipeRefreshLayout只能實(shí)現(xiàn)下拉刷新,而不能實(shí)現(xiàn)上拉加載更多。所以這需要對(duì)其進(jìn)行擴(kuò)充。
在甘南等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷(xiāo),成都外貿(mào)網(wǎng)站建設(shè),甘南網(wǎng)站建設(shè)費(fèi)用合理。
1、首先繼承SwipeRefreshLayout
public class SwipeRefreshAndMoreLoadLayout extends SwipeRefreshLayout implements OnScrollListener {
接下來(lái)直接替代碼了
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 { /** * 滑動(dòng)到最下面時(shí)的上拉操作 */ private int mTounchslop; /** * ListView的加載中footer */ private View mListViewFooter; private ListView mListView; /** * 按下時(shí)的y坐標(biāo) */ 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對(duì)象 */ 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; // 設(shè)置滾動(dòng)監(jiān)聽(tīng)器給ListView, 使得滾動(dòng)的情況下也可以自動(dòng)加載 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
分享文章:使用SwipeRefreshLayout實(shí)現(xiàn)下拉刷新與上拉加載更多
轉(zhuǎn)載源于:http://muchs.cn/article42/pjpohc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站策劃、企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)