Android RecyclerView实现下拉刷新和上拉加载

2019-12-10 18:00:17于海丽

05-10 10:30:34.906 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
..........................................................................
05-10 10:30:35.086 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
05-10 10:30:35.106 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onRefresh
05-10 10:30:37.116 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onComplete
05-10 10:30:37.416 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
..........................................................................
05-10 10:30:37.516 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
05-10 10:30:37.916 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onReset

首先会调用onPrepare()方法,onMove()方法会一直调用,只要视图有偏移,就会调用。下拉到一定距离之后,松开调用onRelaease(),回归到刷新位置时回调onRefresh(),加载完成调用onComplete(),视图开始缩小,最后隐藏之后调用onReset()

根据需求自定义视图,

Android,RecyclerView,下拉刷新,上拉加载

定义我们的椭圆,使用自定义控件

 

/**
 * CircleView 圆盘控件,可以旋转
 * Created by Alex_MaHao on 2016/5/10.
 */
public class CircleView extends View {

  /**
   * 控件的半径
   */
  private int mRadius;

  /**
   * 绘制弧形的画笔
   */
  private Paint mArcPaint;

  /**
   * 绘制弧形的区域
   */
  private RectF mRange;


  private int[] colors = {Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN};

  public CircleView(Context context) {
    this(context, null, 0);
  }

  public CircleView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    init();
  }

  private void init() {
    mArcPaint = new Paint();
    mArcPaint.setAntiAlias(true);
    mArcPaint.setDither(true);
    mArcPaint.setStyle(Paint.Style.FILL);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = 0;
    int height = 0;

    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);


    if (widthMode == MeasureSpec.EXACTLY) {
      width = widthSize;
    } else {
      width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
    }

    if (heightMode == MeasureSpec.EXACTLY) {
      height = heightSize;
    } else {
      height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
    }

    //获取半径
    mRadius = Math.min(width, height) / 2;
    /**
     * 设置宽高为固定值
     */
    setMeasuredDimension(mRadius * 2, mRadius * 2);

     mRange = new RectF(0, 0, mRadius * 2, mRadius * 2);
  }


  @Override
  protected void onDraw(Canvas canvas) {

    float degree = 360/colors.length/2f;

    for (int i = 0; i < 8; i++) {
      mArcPaint.setColor(colors[i%4]);
      canvas.drawArc(mRange,-90f+degree*i,degree,true,mArcPaint);
    }

  }
}