我想亲手做一个刷新加载控件

其实很久以前就想自己实现一个下拉刷新上拉加载控件,之前一直用的是开源的控件,从一开始的PullToRefreshView到后来的SwipeRefreshLayout。虽然功能上都能实现,但用别人的总感觉不得劲。看到别的app上用的炫酷的刷新加载控件就是心痒痒。

1.美团的是这样的

美团.jpg
美团.jpg

2.京东的是这样的

S61028-162146.jpg

3.新浪微博的是这样的

微博.jpg
微博.jpg

看着都挺炫酷的,那咱们是不是也可以搞一个。既然要搞当然是先选一个简单的来搞,毕竟谁都喜欢捡软柿子捏。那哪个最简单呐,不管你们认为是哪个反正我觉得微博的看起来简单一点。昨天下午趁着有点时间简单的实现了一下微博的刷新加载效果。先别急,咱一步一步慢慢来。首先观察一下整体结构,界面分成三部分。

  • 头部刷新布局
  • 可滚动控件(ListView,RecycleView)
  • 尾部加载布局

可以看成刚开始的时候头部刷新布局和尾部都藏在可滚动布局的下面,当滚动到上下边界的时候才一点点显示出来,很容易想到通过继承FrameLayout实现。为了提高扩展性我们在真正的头部布局和尾部布局外面再套一层布局,看下面的具体代码就能明白。在onAttachedToWindow方法中创建外层头部尾部布局最为合适,这时候自定义的控件已经附着到根布局上。
<pre>
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

    //添加头部
    if (mHeadLayout == null) {
        FrameLayout headViewLayout = new FrameLayout(getContext());
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
        layoutParams.gravity = Gravity.TOP;
        headViewLayout.setLayoutParams(layoutParams);
        headViewLayout.setBackgroundColor(Color.parseColor("#F2F2F2"));
        mHeadLayout = headViewLayout;
        this.addView(mHeadLayout);
    }

    //添加底部
    if (mBottomLayout == null) {
        FrameLayout bottomViewLayout = new FrameLayout(getContext());
        LayoutParams layoutParams2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
        layoutParams2.gravity = Gravity.BOTTOM;
        bottomViewLayout.setLayoutParams(layoutParams2);
        bottomViewLayout.setBackgroundColor(Color.parseColor("#F2F2F2"));
        mBottomLayout = bottomViewLayout;
        this.addView(mBottomLayout);
    }

    mChildView = getChildAt(0);
    if (mChildView == null) return;
}

</pre>

加上mHeadLayout == null和mBottomLayout == null的判断是因为执行onResume会重新触发onAttachedToWindow()重复创建HeadLayout和BottomLayout,接下就要分析刷新和加载的状态
刷新状态:

  • 下拉刷新
  • 释放刷新 (箭头旋转)
  • 正在刷新(隐藏箭头,显示刷新动画)

加载状态:

  • 加载中
  • 加载结束

对应的头部刷新控件如下
<pre>
public class SinaRefreshView extends FrameLayout implements IHeaderView{
private ImageView refreshArrow;
private ImageView loadingView;
private TextView refreshTextView;

public SinaRefreshView(Context context) {
    this(context, null);
}

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

public SinaRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init(){
    View rootView = View.inflate(getContext(), R.layout.layout_sinaheader, null);
    refreshArrow = (ImageView) rootView.findViewById(R.id.iv_arrow);
    refreshTextView = (TextView) rootView.findViewById(R.id.tv);
    loadingView = (ImageView) rootView.findViewById(R.id.iv_loading);
    addView(rootView);
}

private String pullDownStr = "下拉刷新";
private String releaseRefreshStr = "释放刷新";
private String refreshingStr = "正在刷新";

@Override
public void onPullingDown(float fraction, float headHeight) {
    if (fraction < 1f) refreshTextView.setText(pullDownStr);
    if (fraction > 1f) refreshTextView.setText(releaseRefreshStr);
    refreshArrow.setRotation(fraction  * 180);
}

@Override
public void onPullReleasing(float fraction, float headHeight) {
    if (fraction < 1f) {
        refreshTextView.setText(pullDownStr);
        refreshArrow.setRotation(fraction * 180);
        if (refreshArrow.getVisibility() == GONE) {
            refreshArrow.setVisibility(VISIBLE);
            loadingView.setVisibility(GONE);
        }
    }
}

@Override
public void startAnim() {
    refreshTextView.setText(refreshingStr);
    refreshArrow.setVisibility(GONE);
    loadingView.setVisibility(VISIBLE);
    ((AnimationDrawable)loadingView.getDrawable()).start();
}

}
</pre>

对应的布局
<pre>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
>

<ImageView
    android:id="@+id/iv_arrow"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:src="@drawable/ic_arrow"/>

<ImageView
    android:id="@+id/iv_loading"
    android:layout_width="34dp"
    android:layout_height="34dp"
    android:src="@drawable/anim_loading_view"
    android:visibility="gone"/>

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:text="下拉刷新"
    android:textSize="16sp"/>

</LinearLayout>
</pre>

对应的尾部加载控件
<pre>
public class LoadingView extends ImageView implements IBottomView{
public LoadingView(Context context) {
this(context, null);
}

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

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

    int size = SettingUtil.dip2px(context,48);
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(size,size);
    params.gravity = Gravity.CENTER;
    setLayoutParams(params);
    setImageResource(R.drawable.anim_loading_view);
}

@Override
public void startAnim() {
    ((AnimationDrawable)getDrawable()).start();
}

@Override
public void onFinish() {
    ((AnimationDrawable)getDrawable()).stop();
}

}
</pre>

接下来设置头部布局和尾部布局,布局这一块算是彻底搞定了
<pre>
public void setHeaderView(final IHeaderView headerView) {
if (headerView != null) {
mHeadLayout.removeAllViewsInLayout();
mHeadLayout.addView(headerView.getView());
mHeadView = headerView;
}
}

public void setBottomView(final IBottomView bottomView) {
    if (bottomView != null) {
        mBottomLayout.removeAllViewsInLayout();
        mBottomLayout.addView(bottomView.getView());
        mBottomView = bottomView;
    }
}

</pre>

然后就是重点了,分析触摸事件,这里用到两个方法onInterceptTouchEvent和onTouchEvent,什么时候打断触摸事件的传递,自己消耗

  • 滚动到上边界,滚动的view无法再滚动
  • 滚动到下边界,滚动的view无法再滚动

<pre>
/**
* 拦截触摸事件
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mTouchY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float dy = event.getY() - mTouchY;
if (dy > 0 && !ScrollingUtil.canChildScrollUp(mChildView)){
state = PULL_DOWN_REFRESH;
return true;
}else if (dy < 0 && !ScrollingUtil.canChildScrollDown(mChildView)){
state = PULL_UP_LOAD;
return true;
}
break;
case MotionEvent.ACTION_UP:

            break;
    }
    return super.onInterceptTouchEvent(event);
}

</pre>

这里可以通过ViewCompat.canScrollVertically(mChildView, 1);来判断滚动的view是否可以再滚动。接下来就是消耗触摸事件。
处于下拉刷新状态的情况下:

  • 随着手指的下拉,同步刷新头部布局的高度,以及通过setTranslationY()使得滚动控件在Y方向上偏移,同时设置头部控件的状态。
  • 手指释放时判断头部布局的高度是否达到了刷新要求,若没有达到刷新要求直接回滚。若达到了刷新要求先回到刷新高度,然后执行刷新动画以及刷新回调。

处于上拉加载的情况的:

  • 随着手指的下拉,当下拉距离小于设置的尾部高度时,同步刷新尾部布局的高度,以及通过setTranslationY()使得滚动控件在Y方向上偏移,同时设置尾部控件的状态。
  • 手指释放时判断尾部布局的高度是否达到了加载要求,若没有达到加载要求直接回滚。若达到了加载要求,然后执行加载动画以及加载回调。
    <pre>
    /**
    • 触摸事件
      */
      @Override
      public boolean onTouchEvent(MotionEvent event) {
      if (isRefreshing || isLoadingmore) return super.onTouchEvent(event);

      switch (event.getAction()){
      case MotionEvent.ACTION_MOVE:
      float dy = event.getY() - mTouchY;
      float offsetY = dy/2;
      if (state == PULL_DOWN_REFRESH) {
      dy = Math.max(0, offsetY);
      mChildView.setTranslationY(dy);
      mHeadLayout.getLayoutParams().height = (int) dy;
      mHeadLayout.requestLayout();
      if(dy/refreshHeadHeight<1.02)
      mHeadView.onPullingDown(dy/refreshHeadHeight,refreshHeadHeight);
      }else if (state == PULL_UP_LOAD) {
      if(offsetY>0){
      break;
      }
      dy = Math.min(bottomHeight, Math.abs(offsetY));
      dy = Math.max(0, dy);
      mChildView.setTranslationY(-dy);
      mBottomLayout.getLayoutParams().height = (int)dy;
      mBottomLayout.requestLayout();
      }
      break;
      case MotionEvent.ACTION_UP:
      if (state == PULL_DOWN_REFRESH) {
      if(mChildView.getTranslationY() >= refreshHeadHeight){
      animChildView(refreshHeadHeight);
      isRefreshing = true;
      mHeadView.startAnim();
      if(mOnRefreshListener!=null)
      mOnRefreshListener.onRefresh(LXSRefreshLayout.this);
      }else{
      animChildView(0);
      }
      } else if (state == PULL_UP_LOAD) {
      if(Math.abs(mChildView.getTranslationY()) >= bottomHeight){
      isLoadingmore = true;
      mBottomView.startAnim();
      animChildView(-bottomHeight);
      if(mOnRefreshListener!=null)
      mOnRefreshListener.onLoadMore(LXSRefreshLayout.this);
      }else{
      animChildView(0);
      }
      }
      break;
      }
      return super.onTouchEvent(event);
      }
      </pre>

这里有两个注意点:

  • 当处于刷新和加载状态下时消耗掉触摸事件
  • move过程需要判断一下是否滑动到了边缘,不然会有问题

当没有达到刷新或则加载要求的时候需要回滚,直接通过mChildView.setTranslationY(0)太过于生硬,这边通过属性动画过度
<pre>
private void animChildView(float endValue, long duration) {
ObjectAnimator oa = ObjectAnimator.ofFloat(mChildView, "translationY", mChildView.getTranslationY(), endValue);
oa.setDuration(duration);
oa.setInterpolator(new DecelerateInterpolator());//设置速率为递减
oa.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int height = (int) mChildView.getTranslationY();
if (state == PULL_DOWN_REFRESH) {
mHeadLayout.getLayoutParams().height = height;
mHeadLayout.requestLayout();
mHeadView.onPullReleasing(height/refreshHeadHeight,refreshHeadHeight,refreshHeadHeight);
}else if (state == PULL_UP_LOAD) {
mBottomLayout.getLayoutParams().height = -height;
mBottomLayout.requestLayout();
}
}
});
oa.start();
}
</pre>

最后是设置回调接口以及刷险加载结束的处理方法
<pre>
/**
* 刷新结束
*/
public void finishRefreshing() {
isRefreshing = false;
if (mChildView != null) {
animChildView(0f);
}
}

/**
 * 加载更多结束
 */
public void finishLoadmore() {
    isLoadingmore = false;
    if (mChildView != null) {
        animChildView(0f);
        mBottomView.onFinish();
    }
}

public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
    mOnRefreshListener = onRefreshListener;
}

public interface OnRefreshListener{
    void onRefresh(LXSRefreshLayout refreshLayout);
    void onLoadMore(LXSRefreshLayout refreshLayout);
}

</pre>

使用方法跟SwipeRefreshLayout一模一样,运行起来的效果大概是这样的:

刷新加载-1.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,999评论 4 368
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,102评论 1 302
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,709评论 0 250
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,439评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,846评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,881评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,062评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,783评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,517评论 1 248
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,762评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,241评论 1 264
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,568评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,236评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,145评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,941评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,965评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,802评论 2 275

推荐阅读更多精彩内容