文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2023-05-31 02:17

关注

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

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

思路:

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

public class RefreshLayout extends RelativeLayout {    private final int V_REFRESH = 2;    private boolean mIsRefreshDuring;    private boolean mCanDownPull;    private boolean mCanUpPull;    private boolean mIsFirstMove;    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)   {      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;  }}

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯