安卓自带刷新SwipeRefreshLayout 上拉加载更多 自动刷新

之前分享主要是系统自带的SwipeRefreshLayout ,但是只支持下拉刷新,没有加载更多功能 ,但是不知道什么原因官方SwipeRefreshLayout只提供下拉刷新功能,很多时候我们需要上拉刷新功能,但有的时候我们也需要上拉加载更多更能,有需求就会有市场,所以上拉加载应运而生。

添加上拉加载更多功能,,实现SwipeRefreshLayout的自动刷新, 类似知乎安卓版、优酷安卓版一打开页面就自动刷新加载的效果。如存在什么问题请指正。

代码如下:

package com.example.swiperefreshlayoutdemo;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import android.annotation.SuppressLint;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.ListView;/*** 继承自SwipeRefreshLayout,从而实现滑动到底部时上拉加载更多的功能.*/public class RefreshLayout extends SwipeRefreshLayout implementsOnScrollListener {/*** 滑动到最下面时的上拉操作*/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);}@SuppressLint("InflateParams")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);}@Overrideprotected 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 childs = getChildCount();if (childs > 0) {View childView = getChildAt(0);if (childView instanceof ListView) {mListView = (ListView) childView;// 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载mListView.setOnScrollListener(this);Log.d(VIEW_LOG_TAG, "### 找到listview");}}}/** (non-Javadoc)** @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent)*/@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {final int action = event.getAction();switch (action) {case MotionEvent.ACTION_DOWN:// 按下mYDown = (int) event.getRawY();break;case MotionEvent.ACTION_MOVE:// 移动mLastY = (int) event.getRawY();break;case MotionEvent.ACTION_UP:// 抬起if (canLoad()) {loadData();}break;default:break;}return super.dispatchTouchEvent(event);}/*** 是否可以加载更多, 条件是到了最底部, listview不在加载中, 且为上拉操作.** @return*/private boolean canLoad() {return isBottom() && !isLoading && isPullUp();}/*** 判断是否到了最底部*/private boolean isBottom() {if (mListView != null && mListView.getAdapter() != null) {return mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() – 1);}return false;}/*** 是否是上拉操作** @return*/private boolean isPullUp() {return (mYDown – mLastY) >= mTouchSlop;}/*** 如果到了最底部,而且是上拉操作.那么执行onLoad方法*/private void loadData() {if (mOnLoadListener != null) {// 设置状态setLoading(true);//mOnLoadListener.onLoad();}}/*** @param loading*/public void setLoading(boolean loading) {isLoading = loading;if (isLoading)

{//设置ListViewFooter 不可点击mListView.addFooterView(mListViewFooter, null, false);}

else

我诚恳地希望能够获得你的原谅。只是你懂得的,对于有一些委屈,

安卓自带刷新SwipeRefreshLayout 上拉加载更多 自动刷新

相关文章:

你感兴趣的文章:

标签云: