本Demo地址:http://download.csdn.net/download/g_ying_jie/10129660
原作者做了一系列的封装实现了通用的下拉刷新和上拉加载更多,讲解的非常详细生动不了解原理的可以点击下面的链接学习。我在其基础上简化出了一个集成的Demo,拷贝几个自定义的View就能实现该功能,完善了禁用刷新和加载的功能,添加了对RecycleView的支持,以适应更多的场景。本篇博文不会再去讲解实现原理
参考文章:http://blog.csdn.net/zhongkejingwang/article/details/38868463
这里以ScrollView作为案例,其他的一样。
package com.dcg.pulltorefresh.view; import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; public class PullableScrollView extends ScrollView implements Pullable { //是否允许上拉加载 private boolean canPullUp = true; //是否允许下拉刷新 private boolean canPullDown = true; public PullableScrollView(Context context) { super(context); } public PullableScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public PullableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean canPullDown() { if (getScrollY() == 0) return canPullDown; else return false; } @Override public boolean canPullUp() { if (getScrollY() >= (getChildAt(0).getHeight() - getMeasuredHeight())) return canPullUp; else return false; } public void setCanPullUp(boolean canPullUp) { this.canPullUp = canPullUp; } public void setCanPullDown(boolean canPullDown) { this.canPullDown = canPullDown; } }该View继承ScrollView,通过Pullable接口通知父布局PullToRefreshLayout实现下拉刷新、上拉加载的功能。这里添加了两个对外的方法控制canPullDown和canPullUp实现对应功能的禁用
@另外一个需要注意的地方PullToRefreshLayout
package com.dcg.explore.view; import android.content.Context; import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; import android.widget.RelativeLayout; import android.widget.TextView; import com.dcg.explore.R; import java.util.Timer; import java.util.TimerTask; /** * 自定义的布局,用来管理三个子控件 * 一个是下拉头 * 一个是包含内容的pullableView(可以是实现Pullable接口的的任何View) * 一个上拉头 */ public class PullToRefreshLayout extends RelativeLayout { public static final String TAG = "PullToRefreshLayout"; // 初始状态 public static final int INIT = 0; // 释放刷新 public static final int RELEASE_TO_REFRESH = 1; // 正在刷新 public static final int REFRESHING = 2; // 释放加载 public static final int RELEASE_TO_LOAD = 3; // 正在加载 public static final int LOADING = 4; // 操作完毕 public static final int DONE = 5; // 当前状态 private int state = INIT; // 刷新回调接口 private OnRefreshListener mListener; // 刷新成功 public static final int SUCCEED = 0; // 刷新失败 public static final int FAIL = 1; // 按下Y坐标,上一个事件点Y坐标 private float downY, lastY; // 下拉的距离。注意:pullDownY和pullUpY不可能同时不为0 public float pullDownY = 0; // 上拉的距离 private float pullUpY = 0; // 释放刷新的距离 private float refreshDist = 200; // 释放加载的距离 private float loadmoreDist = 200; private MyTimer timer; // 回滚速度 public float MOVE_SPEED = 8; // 第一次执行布局 private boolean isLayout = false; // 在刷新过程中滑动操作 private boolean isTouch = false; // 手指滑动距离与下拉头的滑动距离比,中间会随正切函数变化 private float radio = 2; // 下拉箭头的转180°动画 private RotateAnimation rotateAnimation; // 均匀旋转动画 private RotateAnimation refreshingAnimation; // 下拉头 private View refreshView; // 下拉的箭头 private View pullView; // 正在刷新的图标 private View refreshingView; // 刷新结果图标 private View refreshStateImageView; // 刷新结果:成功或失败 private TextView refreshStateTextView; // 上拉头 private View loadmoreView; // 上拉的箭头 private View pullUpView; // 正在加载的图标 private View loadingView; // 加载结果图标 private View loadStateImageView; // 加载结果:成功或失败 private TextView loadStateTextView; // 实现了Pullable接口的View private View pullableView; // 过滤多点触碰 private int mEvents; // 这两个变量用来控制pull的方向,如果不加控制,当情况满足可上拉又可下拉时没法下拉 private boolean canPullDown = true; private boolean canPullUp = true; private Context mContext; /** * 执行自动回滚的handler */ Handler updateHandler = new Handler() { @Override public void handleMessage(Message msg) { // 回弹速度随下拉距离moveDeltaY增大而增大 MOVE_SPEED = (float) (8 + 5 * Math.tan(Math.PI / 2 / getMeasuredHeight() * (pullDownY + Math.abs(pullUpY)))); if (!isTouch) { // 正在刷新,且没有往上推的话则悬停,显示"正在刷新..." if (state == REFRESHING && pullDownY <= refreshDist) { pullDownY = refreshDist; timer.cancel(); } else if (state == LOADING && -pullUpY <= loadmoreDist) { pullUpY = -loadmoreDist; timer.cancel(); } } if (pullDownY > 0) pullDownY -= MOVE_SPEED; else if (pullUpY < 0) pullUpY += MOVE_SPEED; if (pullDownY < 0) { // 已完成回弹 pullDownY = 0; pullView.clearAnimation(); // 隐藏下拉头时有可能还在刷新,只有当前状态不是正在刷新时才改变状态 if (state != REFRESHING && state != LOADING) changeState(INIT); timer.cancel(); requestLayout(); } if (pullUpY > 0) { // 已完成回弹 pullUpY = 0; pullUpView.clearAnimation(); // 隐藏上拉头时有可能还在刷新,只有当前状态不是正在刷新时才改变状态 if (state != REFRESHING && state != LOADING) changeState(INIT); timer.cancel(); requestLayout(); } Log.d("handle", "handle"); // 刷新布局,会自动调用onLayout requestLayout(); // 没有拖拉或者回弹完成 if (pullDownY + Math.abs(pullUpY) == 0) timer.cancel(); } }; public void setOnRefreshListener(OnRefreshListener listener) { mListener = listener; } public PullToRefreshLayout(Context context) { super(context); initView(context); } public PullToRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public PullToRefreshLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } private void initView(Context context) { mContext = context; timer = new MyTimer(updateHandler); rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation( context, R.anim.reverse_anim); refreshingAnimation = (RotateAnimation) AnimationUtils.loadAnimation( context, R.anim.rotating); // 添加匀速转动动画 LinearInterpolator lir = new LinearInterpolator(); rotateAnimation.setInterpolator(lir); refreshingAnimation.setInterpolator(lir); } private void hide() { timer.schedule(5); } /** * 完成刷新操作,显示刷新结果。注意:刷新完成后一定要调用这个方法 */ /** * @param refreshResult PullToRefreshLayout.SUCCEED代表成功,PullToRefreshLayout.FAIL代表失败 */ public void refreshFinish(int refreshResult) { refreshingView.clearAnimation(); refreshingView.setVisibility(View.GONE); switch (refreshResult) { case SUCCEED: // 刷新成功 refreshStateImageView.setVisibility(View.VISIBLE); refreshStateTextView.setText("刷新成功"); refreshStateImageView .setBackgroundResource(R.mipmap.refresh_succeed); break; case FAIL: default: // 刷新失败 refreshStateImageView.setVisibility(View.VISIBLE); refreshStateTextView.setText("刷新失败"); refreshStateImageView .setBackgroundResource(R.mipmap.refresh_failed); break; } if (pullDownY > 0) { // 刷新结果停留1秒 new Handler() { @Override public void handleMessage(Message msg) { changeState(DONE); hide(); } }.sendEmptyMessageDelayed(0, 1000); } else { changeState(DONE); hide(); } } /** * 加载完毕,显示加载结果。注意:加载完成后一定要调用这个方法 * * @param refreshResult PullToRefreshLayout.SUCCEED代表成功,PullToRefreshLayout.FAIL代表失败 */ public void loadmoreFinish(int refreshResult) { loadingView.clearAnimation(); loadingView.setVisibility(View.GONE); switch (refreshResult) { case SUCCEED: // 加载成功 loadStateImageView.setVisibility(View.VISIBLE); loadStateTextView.setText("加载成功"); loadStateImageView.setBackgroundResource(R.mipmap.load_succeed); break; case FAIL: default: // 加载失败 loadStateImageView.setVisibility(View.VISIBLE); loadStateTextView.setText("加载失败"); loadStateImageView.setBackgroundResource(R.mipmap.load_failed); break; } if (pullUpY < 0) { // 刷新结果停留1秒 new Handler() { @Override public void handleMessage(Message msg) { changeState(DONE); hide(); } }.sendEmptyMessageDelayed(0, 1000); } else { changeState(DONE); hide(); } } private void changeState(int to) { state = to; switch (state) { case INIT: // 下拉布局初始状态 refreshStateImageView.setVisibility(View.GONE); refreshStateTextView.setText("下拉刷新"); pullView.clearAnimation(); pullView.setVisibility(View.VISIBLE); // 上拉布局初始状态 loadStateImageView.setVisibility(View.GONE); loadStateTextView.setText("上拉加载更多"); pullUpView.clearAnimation(); pullUpView.setVisibility(View.VISIBLE); break; case RELEASE_TO_REFRESH: // 释放刷新状态 refreshStateTextView.setText("释放立即刷新"); pullView.startAnimation(rotateAnimation); break; case REFRESHING: // 正在刷新状态 pullView.clearAnimation(); refreshingView.setVisibility(View.VISIBLE); pullView.setVisibility(View.INVISIBLE); refreshingView.startAnimation(refreshingAnimation); refreshStateTextView.setText("正在刷新"); break; case RELEASE_TO_LOAD: // 释放加载状态 loadStateTextView.setText("释放立即加载"); pullUpView.startAnimation(rotateAnimation); break; case LOADING: // 正在加载状态 pullUpView.clearAnimation(); loadingView.setVisibility(View.VISIBLE); pullUpView.setVisibility(View.INVISIBLE); loadingView.startAnimation(refreshingAnimation); loadStateTextView.setText("正在加载"); break; case DONE: // 刷新或加载完毕,啥都不做 break; } } /** * 不限制上拉或下拉 */ private void releasePull() { canPullDown = true; canPullUp = true; } /* * (非 Javadoc)由父控件决定是否分发事件,防止事件冲突 * * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent) */ @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getActionMasked()) { case MotionEvent.ACTION_DOWN: downY = ev.getY(); lastY = downY; timer.cancel(); mEvents = 0; releasePull(); break; case MotionEvent.ACTION_POINTER_DOWN: case MotionEvent.ACTION_POINTER_UP: // 过滤多点触碰 mEvents = -1; break; case MotionEvent.ACTION_MOVE: if (mEvents == 0) { if (pullDownY > 0 || (((Pullable) pullableView).canPullDown() && canPullDown && state != LOADING)) { // 可以下拉,正在加载时不能下拉 // 对实际滑动距离做缩小,造成用力拉的感觉 pullDownY = pullDownY + (ev.getY() - lastY) / radio; if (pullDownY < 0) { pullDownY = 0; canPullDown = false; canPullUp = true; } if (pullDownY > getMeasuredHeight()) pullDownY = getMeasuredHeight(); if (state == REFRESHING) { // 正在刷新的时候触摸移动 isTouch = true; } } else if (pullUpY < 0 || (((Pullable) pullableView).canPullUp() && canPullUp && state != REFRESHING)) { // 可以上拉,正在刷新时不能上拉 pullUpY = pullUpY + (ev.getY() - lastY) / radio; if (pullUpY > 0) { pullUpY = 0; canPullDown = true; canPullUp = false; } if (pullUpY < -getMeasuredHeight()) pullUpY = -getMeasuredHeight(); if (state == LOADING) { // 正在加载的时候触摸移动 isTouch = true; } } else releasePull(); } else mEvents = 0; lastY = ev.getY(); // 根据下拉距离改变比例 radio = (float) (2 + 2 * Math.tan(Math.PI / 2 / getMeasuredHeight() * (pullDownY + Math.abs(pullUpY)))); if (pullDownY > 0 || pullUpY < 0) requestLayout(); if (pullDownY > 0) { if (pullDownY <= refreshDist && (state == RELEASE_TO_REFRESH || state == DONE)) { // 如果下拉距离没达到刷新的距离且当前状态是释放刷新,改变状态为下拉刷新 changeState(INIT); } if (pullDownY >= refreshDist && state == INIT) { // 如果下拉距离达到刷新的距离且当前状态是初始状态刷新,改变状态为释放刷新 changeState(RELEASE_TO_REFRESH); } } else if (pullUpY < 0) { // 下面是判断上拉加载的,同上,注意pullUpY是负值 if (-pullUpY <= loadmoreDist && (state == RELEASE_TO_LOAD || state == DONE)) { changeState(INIT); } // 上拉操作 if (-pullUpY >= loadmoreDist && state == INIT) { changeState(RELEASE_TO_LOAD); } } // 因为刷新和加载操作不能同时进行,所以pullDownY和pullUpY不会同时不为0,因此这里用(pullDownY + // Math.abs(pullUpY))就可以不对当前状态作区分了 if ((pullDownY + Math.abs(pullUpY)) > 8) { // 防止下拉过程中误触发长按事件和点击事件 ev.setAction(MotionEvent.ACTION_CANCEL); } break; case MotionEvent.ACTION_UP: if (pullDownY > refreshDist || -pullUpY > loadmoreDist) // 正在刷新时往下拉(正在加载时往上拉),释放后下拉头(上拉头)不隐藏 { isTouch = false; } if (state == RELEASE_TO_REFRESH) { changeState(REFRESHING); // 刷新操作 if (mListener != null) mListener.onRefresh(this); } else if (state == RELEASE_TO_LOAD) { changeState(LOADING); // 加载操作 if (mListener != null) mListener.onLoadMore(this); } hide(); default: break; } // 事件分发交给父类 super.dispatchTouchEvent(ev); return true; } /** * @author chenjing 自动模拟手指滑动的task */ private class AutoRefreshAndLoadTask extends AsyncTask<Integer, Float, String> { @Override protected String doInBackground(Integer... params) { while (pullDownY < 4 / 3 * refreshDist) { pullDownY += MOVE_SPEED; publishProgress(pullDownY); try { Thread.sleep(params[0]); } catch (InterruptedException e) { e.printStackTrace(); } } return null; } @Override protected void onPostExecute(String result) { changeState(REFRESHING); // 刷新操作 if (mListener != null) mListener.onRefresh(PullToRefreshLayout.this); hide(); } @Override protected void onProgressUpdate(Float... values) { if (pullDownY > refreshDist) changeState(RELEASE_TO_REFRESH); requestLayout(); } } /** * 自动刷新 */ public void autoRefresh() { AutoRefreshAndLoadTask task = new AutoRefreshAndLoadTask(); task.execute(20); } /** * 自动加载 */ public void autoLoad() { pullUpY = -loadmoreDist; requestLayout(); changeState(LOADING); // 加载操作 if (mListener != null) mListener.onLoadMore(this); } private void initView() { // 初始化下拉布局 pullView = refreshView.findViewById(R.id.pull_icon); refreshStateTextView = (TextView) refreshView .findViewById(R.id.state_tv); refreshingView = refreshView.findViewById(R.id.refreshing_icon); refreshStateImageView = refreshView.findViewById(R.id.state_iv); // 初始化上拉布局 pullUpView = loadmoreView.findViewById(R.id.pullup_icon); loadStateTextView = (TextView) loadmoreView .findViewById(R.id.loadstate_tv); loadingView = loadmoreView.findViewById(R.id.loading_icon); loadStateImageView = loadmoreView.findViewById(R.id.loadstate_iv); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { Log.d("Test", "Test"); if (!isLayout) { // 这里是第一次进来的时候做一些初始化 refreshView = getChildAt(0); pullableView = getChildAt(1); loadmoreView = getChildAt(2); isLayout = true; initView(); refreshDist = ((ViewGroup) refreshView).getChildAt(0) .getMeasuredHeight(); loadmoreDist = ((ViewGroup) loadmoreView).getChildAt(0) .getMeasuredHeight(); } // 改变子控件的布局,这里直接用(pullDownY + pullUpY)作为偏移量,这样就可以不对当前状态作区分 refreshView.layout(0, (int) (pullDownY + pullUpY) - refreshView.getMeasuredHeight(), refreshView.getMeasuredWidth(), (int) (pullDownY + pullUpY)); pullableView.layout(0, (int) (pullDownY + pullUpY), pullableView.getMeasuredWidth(), (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight()); loadmoreView.layout(0, (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight(), loadmoreView.getMeasuredWidth(), (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight() + loadmoreView.getMeasuredHeight()); } class MyTimer { private Handler handler; private Timer timer; private MyTask mTask; public MyTimer(Handler handler) { this.handler = handler; timer = new Timer(); } public void schedule(long period) { if (mTask != null) { mTask.cancel(); mTask = null; } mTask = new MyTask(handler); timer.schedule(mTask, 0, period); } public void cancel() { if (mTask != null) { mTask.cancel(); mTask = null; } } class MyTask extends TimerTask { private Handler handler; public MyTask(Handler handler) { this.handler = handler; } @Override public void run() { handler.obtainMessage().sendToTarget(); } } } /** * 刷新加载回调接口 * * @author chenjing */ public interface OnRefreshListener { /** * 刷新操作 */ void onRefresh(PullToRefreshLayout pullToRefreshLayout); /** * 加载操作 */ void onLoadMore(PullToRefreshLayout pullToRefreshLayout); } } PullToRefresh的子布局只能是三个:下拉展示的头布局;ScrollView(或者其他实现了Pullable接口的View)内容布局通知父布局刷新、加载;上拉展示的脚布局。
所以你现在这些之外展示titlebar之类的布局,可以在PullToRefresh外层再放一层布局,如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:id="@+id/rl_title" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorPrimary"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="我来组成标题" android:textSize="18sp" /> </RelativeLayout> <com.dcg.pulltorefresh.view.PullToRefreshLayout android:id="@+id/refresh_view" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/refresh_head" /> <!-- 支持所有实现Pullable接口的View --> <com.dcg.pulltorefresh.view.PullableScrollView android:id="@+id/pull_scroll_view" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:text="开放平台" android:textSize="16sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="18dp" android:layout_marginTop="36dp" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:id="@+id/img_publish_exponent" android:layout_width="60dp" android:layout_height="60dp" android:scaleType="center" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="出版指数" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:id="@+id/img_hot_application" android:layout_width="60dp" android:layout_height="60dp" android:scaleType="center" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="热门应用" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:id="@+id/img_preference_works" android:layout_width="60dp" android:layout_height="60dp" android:scaleType="center" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="优选作品" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:id="@+id/img_open_resource" android:layout_width="60dp" android:layout_height="60dp" android:scaleType="center" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开放资源" android:textSize="14sp" /> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="12dp" android:background="@android:color/darker_gray" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="10dp" android:layout_marginTop="18dp" android:text="热点资讯" android:textSize="16sp" /> <com.dcg.pulltorefresh.view.NoScrollListView android:id="@+id/lv_open_platform" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:dividerHeight="1dp" /> </LinearLayout> </com.dcg.pulltorefresh.view.PullableScrollView> <include layout="@layout/load_more" /> </com.dcg.pulltorefresh.view.PullToRefreshLayout> </LinearLayout>@最后,怎么去使用禁用下拉刷新或者上拉加载
package com.dcg.pulltorefresh; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.dcg.pulltorefresh.view.PullToRefreshLayout; import com.dcg.pulltorefresh.view.PullableScrollView; public class MainActivity extends AppCompatActivity implements PullToRefreshLayout.OnRefreshListener { private PullableScrollView scrollView; private int currentPage = 1; private int totalPage = 5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((PullToRefreshLayout) findViewById(R.id.refresh_view)).setOnRefreshListener(this); scrollView = (PullableScrollView) findViewById(R.id.pull_scroll_view); //禁用下拉刷新 //scrollView.setCanPullDown(false); //禁用上拉加载 //scrollView.setCanPullUp(false); } @Override public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) { // 下拉刷新操作 new Handler() { @Override public void handleMessage(Message msg) { // 千万别忘了告诉控件刷新完毕! pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED); } }.sendEmptyMessageDelayed(0, 5000); } @Override public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) { currentPage++; // 加载操作 new Handler() { @Override public void handleMessage(Message msg) { // 千万别忘了告诉控件加载完毕! pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED); //执行分页加载更多操作 } }.sendEmptyMessageDelayed(0, 5000); if (currentPage == totalPage) { scrollView.setCanPullUp(false); } } }@最后放上改造的可实现上拉加载下拉刷新的RecycleView,详细的用法可以参考demo的RecycleViewActivity
package com.dcg.pulltorefresh.view; import android.content.Context; import android.support.annotation.Nullable; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; /** * Created by Gu on 2017/11/22. */ public class PullableRecycleView extends RecyclerView implements Pullable { private boolean canPullUp = true; private boolean canPullDown = true; private LinearLayoutManager layoutManager; public PullableRecycleView(Context context) { super(context); } public PullableRecycleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public PullableRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean canPullDown() { if (layoutManager.getItemCount() == 0) { // 没有item的时候也可以下拉刷新 return canPullDown; } else if (layoutManager.findFirstVisibleItemPosition() == 0 && getChildAt(0).getTop() >= 0) { // 滑到ListView的顶部了 return canPullDown; } else return false; } @Override public boolean canPullUp() { if (layoutManager.getItemCount() == 0) { // 没有item的时候也可以上拉加载 return canPullUp; } else if (layoutManager.getChildCount() > 0 && layoutManager.findLastVisibleItemPosition() == (layoutManager.getItemCount() - 1)) { // 滑到底部了 if (getChildAt(layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition()) != null && getChildAt(layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition()).getBottom() <= getMeasuredHeight()) return canPullUp; } return false; } public void setCanPullUp(boolean canPullUp) { this.canPullUp = canPullUp; } public void setCanPullDown(boolean canPullDown) { this.canPullDown = canPullDown; } public void setPullable(RecyclerView.LayoutManager layoutManager) { if (layoutManager instanceof LinearLayoutManager) this.layoutManager = (LinearLayoutManager) layoutManager; } }@搞定收工,很多复杂的布局比如ScrollView嵌套GridView、横向的recycleview,listview并要求实现listview的分页加载都可以通过这种方式轻量级的集成实现