SwipeRefreshLayout实现上拉加载
生活随笔
收集整理的這篇文章主要介紹了
SwipeRefreshLayout实现上拉加载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原來的Android SDK中并沒有下拉刷新組件,但是這個組件確實絕大多數APP必備的一個部件。好在google在v4包中出了一個SwipeRefreshLayout,但是這個組件只支持下拉刷新,不支持上拉加載更多的操作。因此,我們就來簡單的擴展一下這個組件以實現上拉下載的目的。
基本原理
上拉加載或者說滾動到底部時自動加載,都是通過判斷是否滾動到了ListView或者其他View的底部,然后觸發相應的操作,這里我們以ListView來說明。因此我們需要在監聽ListView的滾動事件,當ListView滾動到底部時自動觸發加載操作;但是當用戶支持手指滑動屏幕,沒有滾動時,我們也需要讓它加載,因此這種情形就是上拉加載更多。所以我們需要在觸摸事件里面進行判斷,如果到了底部,且用戶是上拉操作,那么執行加載更多。
時間有限,直接上代碼吧。
實現代碼
/*** 繼承自SwipeRefreshLayout,從而實現滑動到底部時上拉加載更多的功能.* * @author mrsimple*/ public class RefreshLayout extends SwipeRefreshLayout implements OnScrollListener { /** * 滑動到最下面時的上拉操作 */ private int mTouchSlop; /** * listview實例 */ private ListView mListView; /** * 上拉監聽器, 到了最底部的上拉加載操作 */ private OnLoadListener mOnLoadListener; /** * ListView的加載中footer */ private View mListViewFooter; /** * 按下時的y坐標 */ private int mYDown; /** * 抬起時的y坐標, 與mYDown一起用于滑動到底部時判斷是上拉還是下拉 */ private int mLastY; /** * 是否在加載中 ( 上拉加載更多 ) */ private boolean isLoading = false; /** * @param context */ public RefreshLayout(Context context) { this(context, null); } public RefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); mListViewFooter = LayoutInflater.from(context).inflate(R.layout.listview_footer, null, false); }listview_footer.xml:
使用示例
refresh.xml布局文件:
activity中的使用 :
/*** @author mrsimple*/ public class MainActivity extends Activity {效果圖
參考:http://www.codeceo.com/article/android-swiperefreshlayout.html http://blog.csdn.net/ueryueryuery/article/details/17440465 http://blog.csdn.net/ljx19900116/article/details/41648965 http://blog.csdn.net/ozackyg/article/details/40509213轉載于:https://www.cnblogs.com/manmanlu/p/5736856.html
總結
以上是生活随笔為你收集整理的SwipeRefreshLayout实现上拉加载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS识别不同浏览器信息
- 下一篇: 2016 Multi-Universit