亲自动手编写Android通用刷新控件

亲自动手编写Android通用刷新控件,第1张

概述项目中我们经常有上拉、下拉刷新的需求,几乎所有的listView、RecyclerView都会伴随着上拉、下拉刷新的需求,如果我们使用一些开源控件,换了控件我们就要更新,现在我们自己撸起袖子写一个通用的刷新控件

项目中我们经常有上拉、下拉刷新的需求,几乎所有的ListVIEw、RecyclerVIEw都会伴随着上拉、下拉刷新的需求,如果我们使用一些开源控件,换了控件我们就要更新,现在我们自己撸起袖子写一个通用的刷新控件

项目地址:https://git.oschina.net/qiangshen/commentview.git

思路:

写一个继承relativeLayout的RefreshLayout 添加头尾控件作为刷新控件 通过事件分发来进行刷新 *** 作 通过动画来控制控件移动

目的:让他的所有子控件都可以使用,哪怕是一个TextVIEw

public class RefreshLayout extends relativeLayout {  /**   * 滑动控件时拉去的速度比例   */  private final int V_REFRESH = 2;  /**   * 是否是刷新过程   * true 是   * false 不是   * 为false的时候才可以进行刷新   */  private boolean mIsRefreshDuring;  /**   * 可以进下拉刷新   */  private boolean mCanDownPull;  /**   * 可以进行上拉刷新   */  private boolean mCanUpPull;  /**   * 判断触摸后是否是初次移动   */  private boolean mIsFirstMove;  /**   * y轴呢平移的距离   */  private int mdistanceY;  /**   * 刷新接口对象   */  private OnRefresh mOnRefresh;  /**   * 用于控制事件拦截的变量   */  private boolean mCanIntercept;  private int mtouchSlop;  private int mdistance;  private LayoutParams mheaderParams;  private VIEw mheaderVIEw;  private VIEw mFootVIEw;  private int mheaderMaxHeight;  private int mStartY;  private LayoutParams mFootParams;  private int mFootMaxHeight;  private PullCallBack mCallBack;  private VIEw mChildVIEw;  private ObjectAnimator mAnimator;  public RefreshLayout(Context context) {    super(context);    initData();  }  public RefreshLayout(Context context,AttributeSet attrs) {    super(context,attrs);    initData();  }  public RefreshLayout(Context context,AttributeSet attrs,int defStyleAttr) {    super(context,attrs,defStyleAttr);    initData();  }  /**   * 必须让头尾控件实现的接口   */  public interface headAndFootCallBack {    //设置属性    voID setAttribute();    //开始刷新    voID startPull();    //停止刷新    voID stopPull();  }  /**   * 必须让被拖动的控件子类实现   */  public interface PullCallBack {    boolean canDownPull();    boolean canUpPull();  }  private voID initData() {    //不调用该方法不能进行绘制    setwillNotDraw(false);  }  /**   * 下拉刷新完成后必须使用该方法   */  public voID downPullFinish() {    mAnimator.setfloatValues(mChildVIEw.getTranslationY(),0);    mAnimator.start();    ((headAndFootCallBack) mheaderVIEw).stopPull();  }  /**   * 上拉完成后必须调用该方法   */  public voID upPullFinish() {    mAnimator.setfloatValues(mChildVIEw.getTranslationY(),0);    mAnimator.start();    ((headAndFootCallBack) mFootVIEw).stopPull();  }  /**   * 自动下拉刷新   */  public voID autoDownPullForhead() {    postDelayed(new Runnable() {      @OverrIDe      public voID run() {        mCanDownPull = true;        mCanUpPull = false;        mAnimator.setfloatValues(10,mheaderMaxHeight);        mAnimator.start();        ((headAndFootCallBack) mheaderVIEw).startPull();        mOnRefresh.onDownPullRefresh();      }    },500);  }  /**   * 自动下拉刷新   */  public voID autoUpPullForhead() {    postDelayed(new Runnable() {      @OverrIDe      public voID run() {        mCanDownPull = false;        mCanUpPull = true;        mAnimator.setfloatValues(0,mFootMaxHeight);        mAnimator.start();        ((headAndFootCallBack) mFootVIEw).startPull();        mOnRefresh.onUpPullRefresh();      }    },500);  }  @OverrIDe  public boolean onIntercepttouchEvent(MotionEvent ev) {    return mCanIntercept;  }  @OverrIDe  public boolean ontouchEvent(MotionEvent event) {    return true;  }  @OverrIDe  public boolean dispatchtouchEvent(MotionEvent event) {    Log.e("shen","mIsRefreshDuring=" + mIsRefreshDuring);    if (mIsRefreshDuring) /**如果正在进行刷新将不会获取MotionEvent*/  {      return super.dispatchtouchEvent(event);    }    switch (event.getAction()) {      case MotionEvent.ACTION_DOWN:        mStartY = (int) event.getY();        initPull();        break;      case MotionEvent.ACTION_MOVE:        if (event.getPointerCount() == 1) {          int moveY = (int) event.getY();          mdistanceY = (moveY - mStartY) / V_REFRESH;          if (!mIsFirstMove && mdistanceY != 0 && mdistanceY < mtouchSlop) {            mCanDownPull = mdistanceY > 0;            mCanUpPull = !mCanDownPull;            mIsFirstMove = true;          }          if (mCanDownPull && mCallBack.canDownPull()) {            upDataForDownPull();//下拉刷新            mChildVIEw.setEnabled(false);            mCanIntercept = true;          }          if (mCanUpPull && mCallBack.canUpPull()) {            upDataForUpPull();//上拉加载            mChildVIEw.setEnabled(false);            mCanIntercept = true;          }          mStartY = moveY;        }        break;      case MotionEvent.ACTION_UP:        mIsRefreshDuring = true;        mIsFirstMove = false;        if (mheaderParams.height >= mheaderMaxHeight) /**可以下拉刷新**/  {          ((headAndFootCallBack) mheaderVIEw).startPull();          mOnRefresh.onDownPullRefresh();        } else if (mFootParams.height >= mFootMaxHeight) /**可以上拉刷新**/  {          ((headAndFootCallBack) mFootVIEw).startPull();          mOnRefresh.onUpPullRefresh();        } else if (mheaderParams.height > 0 && mheaderParams.height < mheaderMaxHeight) /**不能进行下拉刷新,收回**/  {          releaseForDownFinished();        } else if (mFootParams.height > 0 && mFootParams.height < mFootMaxHeight) /**不能进行下拉刷新,收回**/  {          releaseForUpFinished();        } else {          mIsRefreshDuring = false;          mCanIntercept = false;        }        break;    }    super.dispatchtouchEvent(event);    return true;  }  /**   * 每次进行触摸都需要进行初始化   */  private voID initPull() {    mCanDownPull = false;    mCanUpPull = false;  }  /**   * 不需要进行上拉刷新   */  private voID releaseForUpFinished() {    mAnimator.setfloatValues(mChildVIEw.getTranslationY(),0);    mAnimator.start();  }  /**   * 不需要进行下拉刷新   */  private voID releaseForDownFinished() {    mAnimator.setfloatValues(mChildVIEw.getTranslationY(),0);    mAnimator.start();  }  /**   * 上拉时处理手势   */  private voID upDataForUpPull() {    if (mdistanceY != 0) {      mFootParams.height -= mdistanceY;      if (mFootParams.height <= 0) {        mFootParams.height = 0;      }      if (mFootParams.height >= mFootMaxHeight) {        mFootParams.height = mFootMaxHeight;      }      mChildVIEw.setTranslationY(-mFootParams.height);      mFootVIEw.requestLayout();    }  }  /**   * 下拉时处理手势   */  private voID upDataForDownPull() {    if (mdistanceY != 0) {      mheaderParams.height += mdistanceY;      if (mheaderParams.height >= mheaderMaxHeight) { //最大        mheaderParams.height = mheaderMaxHeight;      }      if (mheaderParams.height <= 0) { //最小        mheaderParams.height = 0;      }      mChildVIEw.setTranslationY(mheaderParams.height);      mheaderVIEw.requestLayout();    }  }  @OverrIDe  protected voID onAttachedToWindow() {    super.onAttachedToWindow();  }  @OverrIDe  protected voID onFinishInflate() {    super.onFinishInflate();    //加载头    mheaderVIEw = getChildAt(0);    if (!(mheaderVIEw instanceof headAndFootCallBack)) {      new IllegalStateException("headerVIEw必须实现headAndFootCallBack接口");    }    ((headAndFootCallBack) mheaderVIEw).setAttribute();    mheaderParams = (LayoutParams) mheaderVIEw.getLayoutParams();    mheaderParams.addRule(relativeLayout.AliGN_PARENT_top);    //加载尾    mFootVIEw = getChildAt(2);    if (!(mFootVIEw instanceof headAndFootCallBack)) {      new IllegalStateException("FootVIEw必须实现headAndFootCallBack接口");    }    ((headAndFootCallBack) mFootVIEw).setAttribute();    mFootParams = (LayoutParams) mFootVIEw.getLayoutParams();    mFootParams.addRule(relativeLayout.AliGN_PARENT_BottOM);    mChildVIEw = getChildAt(1);    if (!(mChildVIEw instanceof headAndFootCallBack)) {      new IllegalStateException("ChildVIEw必须实现PullCallBack接口");    }    mCallBack = (PullCallBack) getChildAt(1);    //设置动画    mAnimator = ObjectAnimator.offloat(mChildVIEw,"translationY",0);    mAnimator.setInterpolator(new DecelerateInterpolator());    mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {      @OverrIDe      public voID onAnimationUpdate(ValueAnimator animation) {        int translationY = (int) mChildVIEw.getTranslationY();        if (mCanUpPull) { //从移动到的位置往下滑          mFootParams.height = Math.abs(translationY);          mFootVIEw.requestLayout();        } else if (mCanDownPull) {          mheaderParams.height = Math.abs(translationY);          mheaderVIEw.requestLayout();        }        Log.e("shen","translationY=" + translationY);        Log.e("shen","mheaderParams.height=" + mheaderParams.height);        if (translationY == 0) {          mChildVIEw.setEnabled(true);          mdistanceY = 0; //重置          mIsRefreshDuring = false; //重置          mCanIntercept = false;        } else {          mIsRefreshDuring = true;        }      }    });  }  @OverrIDe  protected voID onSizeChanged(int w,int h,int olDW,int oldh) {    super.onSizeChanged(w,h,olDW,oldh);    mtouchSlop = VIEwConfiguration.get(getContext()).getScaledtouchSlop();    mdistance = mtouchSlop * 5;    //设置下拉头初始属性    mheaderMaxHeight = mheaderParams.height;    mheaderParams.height = 0;    mheaderVIEw.requestLayout();    //设置上拉尾初始属性    mFootMaxHeight = mFootParams.height;    mFootParams.height = 0;    mFootVIEw.requestLayout();  }  /**   * 下拉/上拉事件监听   */  public interface OnRefresh {    /**     * 下拉刷新     */    voID onDownPullRefresh();    /**     * 上拉加载     */    voID onUpPullRefresh();  }  public voID setonRefresh(OnRefresh onRefresh) {    mOnRefresh = onRefresh;  }}

给他添加三个控件,头尾就是刷新头、尾,第二个就是正常显示的控件。必须让头尾实现headAndFootCallBack接口,来设置属性,通知开始刷新、结束刷新

难点: 现在来说下开发时遇到的难点

由于判断在dispatchtouchEvent中,导致如果该控件以及子控件都不消费该事件的话,就会造成事件不会发送到它,因为如果不消费DOWN事件的话,之后所有的事件都不会在进行接收。解决方式,让该控件ontouchEvent方法消返回true,当子控件不进行事件消费的话,就会返回由该控件消费,不会造成因DOWN事件不消费而无法接收到事件,导致dispatchtouchEvent也不消费事件
动画,动画就是我的伤痛,最近在学习估值器

这个控件自认为写的不错,通过他可以帮我们学习事件分发、动画、接口回调,也是有一定的学习意义

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的亲自动手编写Android通用刷新控件全部内容,希望文章能够帮你解决亲自动手编写Android通用刷新控件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1146109.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存