Android实现RecyclerView下拉刷新效果

本文为大家分享了Android实现RecyclerView下拉刷新效果的具体代码,供大家参考,具体内容如下

思路

  • RealPullRefreshView继承了一个LinearLayout
  • 里面放置了一个刷新头布局,将其margin_top设置为负的刷新头的高度的
  • 再添加一个RecyclerView
  • 触摸事件分发机制,当在特定条件下让RealPullRefreshView拦截触摸事件,否则的话,不拦截,让RecyclerView自己去处理触摸事件
  • 在手指下拉时,定义好不同的状态STATE,在不同状态下,处理不同的显示,这里讲不同状态下的刷新头如何显示,抽象为一个接口,用户可以实现这个接口,自定义刷新头的布局和动画
  • 加载更多的功能是利用RecyclerView的多type布局实现的
  • 难点在于触摸事件的拦截,和认真处理各种滑动的问题

使用

xml

 <com.example.apple.quickdemo.realview.view.RealPullRefreshView
    android:id="@+id/real_pull_refresh_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:refresh_header_view="@layout/headerview"/>

这是headerview

<?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="50dp"
       android:background="#ff0"
       android:orientation="horizontal"
       android:gravity="center">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:visibility="visible"
    android:id="@+id/tv"
    android:gravity="center"
    android:text="下拉刷新"
    android:textSize="21sp"/>
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:src="@mipmap/ic_launcher"
    android:id="@+id/iv"
    />

</LinearLayout>

代码

mRealPullRefreshView.setLayoutManager(mLayoutManager);
    mRealPullRefreshView.setAdapter(mMyAdapte);
    //用户可以自定义自己的刷新头布局和动画
   //mRealPullRefreshView.setOnPullShowViewListener(new GifOnPullShowViewListerner(mRealPullRefreshView));

    mRealPullRefreshView.setOnPullListener(new RealPullRefreshView.OnPullListener() {
      @Override
      public void onRefresh() {

        mHandler.postDelayed(new Runnable() {
          @Override
          public void run() {
            mBodies.add(0, new Body("新数据"+i++,100));
            mRealPullRefreshView.refreshFinish();
          }
        }, 3000);

      }

      @Override
      public void onLoadMore() {

        final List<Body> more=new ArrayList<Body>();
        mHandler.postDelayed(new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < 3; i++) {
              more.add(new Body("more+++"+i,100));

            }

            mBodies.addAll(more);
            mRealPullRefreshView.loadMreFinish();
          }
        }, 1500);

      }
    });

自定义刷新头布局和动画

package com.example.apple.quickdemo.realview.show;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.apple.quickdemo.R;
import com.example.apple.quickdemo.realview.view.RealPullRefreshView;

/**
 * Created by apple on 2017/7/9.
 */

public class ImplOnPullShowViewListener implements RealPullRefreshView.OnPullShowViewListener {

  private TextView mTv;
  private ImageView mIv;
  private ObjectAnimator mAni;
  View mHeaderView;

  public ImplOnPullShowViewListener(RealPullRefreshView realPullRefreshView) {
    mHeaderView = realPullRefreshView.getRefreshHeaderView();
    mTv = (TextView) mHeaderView.findViewById(R.id.tv);
    mIv = (ImageView) mHeaderView.findViewById(R.id.iv);
    mAni = ObjectAnimator.ofFloat(mIv, "rotation", -15, 15).setDuration(300);
    mAni.setRepeatCount(ValueAnimator.INFINITE);
    mAni.setRepeatMode(ValueAnimator.REVERSE);
  }

  @Override
  public void onPullDownRefreshState(int scrollY, int headviewHeight,int deltaY) {
    mTv.setText("下拉刷新");
    float f = -((float) scrollY / (float) headviewHeight);
    Log.e("tag", f+ "");
    Log.e("tag", -scrollY + "scrollY");

    mIv.setScaleX(f);
    mIv.setScaleY(f);
  }

  @Override
  public void onReleaseRefreshState(int scrollY, int deltaY) {
    mTv.setText("松手刷新");

  }

  @Override
  public void onRefreshingState() {
    mTv.setText("正在刷新");
    mIv.setScaleX(1.0f);
    mIv.setScaleY(1.0f);
    mAni.start();

  }

  @Override
  public void onDefaultState() {
    if (mAni.isRunning()){
      mAni.end();
      mIv.setRotation(0);
    }
  }

}

源码

package com.example.apple.quickdemo.realview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.Toast;

import com.example.apple.quickdemo.R;
import com.example.apple.quickdemo.realview.show.ImplOnPullShowViewListener;

import static android.content.ContentValues.TAG;

/**
 * Created by apple on 2017/7/7.
 * 下拉刷新
 */

public class RealPullRefreshView extends LinearLayout {

  private int mTouchSlop;
  //  分别记录上次滑动的坐标
  private int mLastX = 0;
  private int mLastY = 0;

  //  分别记录上次滑动的坐标(onInterceptTouchEnvent)
  private int mLastXIntercept = 0;
  private int mLastYIntercept = 0;

  private Scroller mScroller;
  private VelocityTracker mVelocityTracker;

  private RecyclerView.Adapter mAdapter;

  public RecyclerView getRecyclerView() {
    return mRecyclerView;
  }

  private RecyclerView mRecyclerView;

  private int DEFAULT = 0;
  private final int PULL_DOWN_REFRESH = 1;
  private final int RELEASE_REFRESH = 2;
  private final int REFRESHING = 3;
  private final int LOAD_MORE = 4;
  private int STATE = DEFAULT;

  private int rfreshHeaderWidth;
  private int refreshHeadviewHeight;

  private OnPullListener mOnPullListener;
  private View mRefreshHeaderView;

  private RecyclerView.LayoutManager mLayoutManager;

  int refreshHeadviewId;

  public void setLayoutManager(RecyclerView.LayoutManager manager) {
    this.mLayoutManager = manager;
    mRecyclerView.setLayoutManager(mLayoutManager);

  }

  public void setAdapter(RecyclerView.Adapter adapter) {
    this.mAdapter = adapter;
    mRecyclerView.setAdapter(mAdapter);

  }

  public View getRefreshHeaderView() {
    return mRefreshHeaderView;
  }

  public void setOnPullShowViewListener(OnPullShowViewListener onPullShowViewListener) {
    mOnPullShowViewListener = onPullShowViewListener;
  }

  private OnPullShowViewListener mOnPullShowViewListener;

  public void setOnPullListener(OnPullListener onPullListener) {
    mOnPullListener = onPullListener;
  }

  public RealPullRefreshView(Context context) {
    super(context);

    initView(context);

  }

  public RealPullRefreshView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);

    initAttrs(context, attrs);
//   ★★★★★一个坑initAttrs方法里的typedArray去获取属性时,第一次获取的属性全是0,他会马上重走一次构造方法,再次获取一次,才能获得正确的值
//   如果第一次获取的值为0,则不去initView
    if (refreshHeadviewId != 0) {
      initView(context);
    }

  }

  public RealPullRefreshView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    initAttrs(context, attrs);
    if (refreshHeadviewId != 0) {
      initView(context);
    }
  }

  private void initAttrs(Context context, AttributeSet attrs) {

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RealPullRefreshView);

    try {

      refreshHeadviewId = typedArray.getResourceId(R.styleable.RealPullRefreshView_refresh_header_view, 0);

    } finally {
      typedArray.recycle();

    }

  }

  private void initView(Context context) {

    mScroller = new Scroller(getContext());
    mVelocityTracker = VelocityTracker.obtain();

//    添加headerview

//    ★ ★ ★ ★ ★ 注意不要用这个方法inflate布局,会导致layout的所有属性失效,height、width、margin
//    原因见 http://blog.csdn.net/zhaokaiqiang1992/article/details/36006467
//    ★ ★ ★ ★ ★ mRefreshHeaderView = mInflater.inflate(R.layout.headerview, null);

    mRefreshHeaderView = LayoutInflater.from(context).inflate(refreshHeadviewId, this, false);
    addView(mRefreshHeaderView);
//    }

//    以下代码主要是为了设置头布局的marginTop值为-headerviewHeight
//    注意必须等到一小会才会得到正确的头布局宽高
    postDelayed(new Runnable() {
      @Override
      public void run() {
        Log.e("q11", refreshHeadviewHeight + "qqqqqqqqqq   " + mRefreshHeaderView.getHeight());
        rfreshHeaderWidth = mRefreshHeaderView.getWidth();
        refreshHeadviewHeight = mRefreshHeaderView.getHeight();

        MarginLayoutParams lp = new LinearLayout.LayoutParams(rfreshHeaderWidth, refreshHeadviewHeight);
        lp.setMargins(0, -refreshHeadviewHeight, 0, 0);
        mRefreshHeaderView.setLayoutParams(lp);

      }
    }, 100);

//   添加RecyclerView
    mRecyclerView = new RecyclerView(context);
    addView(mRecyclerView, LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

//   这里我提供了一个默认的显示效果,如果用户不使用mRealPullRefreshView.setOnPullShowViewListener的话,会默认使用这个
//   用户可以实现OnPullShowViewListener接口,去实现自己想要的显示效果
    mOnPullShowViewListener = new ImplOnPullShowViewListener(this);

    setLoadMore();

  }

  private void setLoadMore() {
    // 当目前的可见条目是所有数据的最后一个时,开始加载新的数据

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        int lastCompletelyVisibleItemPosition = -1;
        if (mLayoutManager instanceof LinearLayoutManager) {
          LinearLayoutManager manager = (LinearLayoutManager) mLayoutManager;
          lastCompletelyVisibleItemPosition = manager.findLastVisibleItemPosition();

        } else if (mLayoutManager instanceof GridLayoutManager) {
          GridLayoutManager manager = (GridLayoutManager) mLayoutManager;
          lastCompletelyVisibleItemPosition = manager.findLastVisibleItemPosition();

        } else if (mLayoutManager instanceof StaggeredGridLayoutManager) {
          StaggeredGridLayoutManager manager = (StaggeredGridLayoutManager) mLayoutManager;
          lastCompletelyVisibleItemPosition = manager.findLastVisibleItemPositions(new int[manager.getSpanCount()])[0];
        }
        if (lastCompletelyVisibleItemPosition + 1 == mAdapter.getItemCount()) {
          if (mOnPullListener != null && STATE == DEFAULT) {
            STATE = LOAD_MORE;
            mOnPullListener.onLoadMore();
          }
        }

      }
    });
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
    boolean intercepted = false;
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN: {
        intercepted = false;
        /*if (STATE!=DEFAULT||STATE!=REFRESHING){
        if (!mScroller.isFinished()) {
          mScroller.abortAnimation();
        }}*/
        break;
      }
      case MotionEvent.ACTION_MOVE: {
        int deltaX = x - mLastXIntercept;
        int deltaY = y - mLastYIntercept;

        int firstCompletelyVisibleItemPosition = -1;
        if (mLayoutManager instanceof LinearLayoutManager) {
          LinearLayoutManager manager = (LinearLayoutManager) mLayoutManager;
          firstCompletelyVisibleItemPosition = manager.findFirstCompletelyVisibleItemPosition();

        } else if (mLayoutManager instanceof GridLayoutManager) {
          GridLayoutManager manager = (GridLayoutManager) mLayoutManager;
          firstCompletelyVisibleItemPosition = manager.findFirstCompletelyVisibleItemPosition();

        } else if (mLayoutManager instanceof StaggeredGridLayoutManager) {
          StaggeredGridLayoutManager manager = (StaggeredGridLayoutManager) mLayoutManager;
          firstCompletelyVisibleItemPosition = manager.findFirstCompletelyVisibleItemPositions(new int[manager.getSpanCount()])[0];
        }

        //        ******************这里说明什么规则下,拦截,其余代码不要动了,其余代码指的是处理滑动冲突的代码***************

        if (firstCompletelyVisibleItemPosition == 0 && deltaY > 0 && Math.abs(deltaY) > Math.abs(deltaX)) {//拉倒最顶部,继续往下拉,将拉出头布局,要父布局拦截
          intercepted = true;
        } else if (getScrollY() < 0) {//表示头布局已经向下拉出来,头布局已经显示了,要父布局拦截
          intercepted = true;

        } else if (deltaY < 0) {
          intercepted = false;//不要父布局拦截了

        } else {
          intercepted = false;//不要父布局拦截了
        }
        //        ******************什么规则下,拦截***************

        break;
      }
      case MotionEvent.ACTION_UP: {
        intercepted = false;
        break;
      }
      default:
        break;
    }

    Log.d(TAG, "intercepted=" + intercepted);
    mLastX = x;
    mLastY = y;
    mLastXIntercept = x;
    mLastYIntercept = y;

    return intercepted;
  }

  /**
   * 下面不同布局,不同的滑动需求
   *
   * @param event
   * @return
   */
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    mVelocityTracker.addMovement(event);
    int x = (int) event.getX();
    int y = (int) event.getY();
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN: {
        if (!mScroller.isFinished()) {
          mScroller.abortAnimation();
        }
        break;
      }
      case MotionEvent.ACTION_MOVE: {
        int deltaX = x - mLastX;
        int deltaY = y - mLastY;
        if (getScrollY() > 0) {
          //防止在正在刷新状态下,上拉出空白
        } else if (getScrollY() <= 0 && getScrollY() > -refreshHeadviewHeight * 5) {
//          最多下拉到头布局高度5倍的距离
          scrollBy(0, -deltaY / 2);
        }

        if (getScrollY() > -refreshHeadviewHeight && STATE != REFRESHING) {//头布局显示不全时,为下拉刷新PULL_DOWN_REFRESH状态
          STATE = PULL_DOWN_REFRESH;

          if (mOnPullShowViewListener != null) {
            mOnPullShowViewListener.onPullDownRefreshState(getScrollY(), refreshHeadviewHeight, deltaY);
          }

        }
        if (getScrollY() < -refreshHeadviewHeight && STATE != REFRESHING) {//头布局完全显示时,为释放刷新RELEASE_REFRESH状态
          STATE = RELEASE_REFRESH;
          if (mOnPullShowViewListener != null) {
            mOnPullShowViewListener.onReleaseRefreshState(getScrollY(), deltaY);
          }

        }

        break;
      }
      case MotionEvent.ACTION_UP: {
        final int scrollY = getScrollY();
        //松手时,根据所处的状态,让布局滑动到不同的地方,做不同的操作
        switch (STATE) {

          case PULL_DOWN_REFRESH:
            STATE = DEFAULT;
            //头布局没有完全显示,完全隐藏头布局
            smoothScrollBy(0, -scrollY);
            break;
          case RELEASE_REFRESH:
            STATE = REFRESHING;
            smoothScrollBy(0, -refreshHeadviewHeight - scrollY);

            if (mOnPullShowViewListener != null) {
              mOnPullShowViewListener.onRefreshingState();
            }

            if (mOnPullListener != null) {
              mOnPullListener.onRefresh();
            }
            break;
          case REFRESHING:
            if (getScrollY() < -refreshHeadviewHeight) {
              smoothScrollBy(0, -refreshHeadviewHeight - scrollY);
            } else {
              smoothScrollBy(0, -scrollY);
            }
            break;
        }

        mVelocityTracker.clear();
        break;
      }
      default:
        break;
    }

    mLastX = x;
    mLastY = y;
    return true;
  }

  /**
   * 当用户使用完下拉刷新回调时,需要调用此方法,将头不去隐藏,将STATE恢复
   */
  public void refreshFinish() {
    smoothScrollBy(0, 0 - getScrollY());
    getRecyclerView().getAdapter().notifyDataSetChanged();
    STATE = DEFAULT;
    if (mOnPullShowViewListener != null) {
      mOnPullShowViewListener.onDefaultState();
    }

    Toast.makeText(getContext(), "刷新成功!", Toast.LENGTH_SHORT).show();
  }

  /**
   * 当用户使用完加载更多后回调时,需要调用此方法,将STATE恢复
   */
  public void loadMreFinish() {
    getRecyclerView().getAdapter().notifyDataSetChanged();
    STATE = DEFAULT;

    Toast.makeText(getContext(), "加载成功了!", Toast.LENGTH_SHORT).show();
  }

  /**
   * 在500毫秒内平滑地滚动多少像素点
   *
   * @param dx
   * @param dy
   */
  private void smoothScrollBy(int dx, int dy) {
    mScroller.startScroll(0, getScrollY(), 0, dy, 500);
    invalidate();
  }

  @Override
  public void computeScroll() {
    if (mScroller.computeScrollOffset()) {
      scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
      postInvalidate();
    }
  }

  /**
   * 释放资源
   */
  @Override
  protected void onDetachedFromWindow() {
    mVelocityTracker.recycle();
    super.onDetachedFromWindow();
  }

//  ***************

//  *****************

  /**
   * 回调接口
   */
  public interface OnPullListener {
    /**
     * 当下拉刷新正在刷新时,这时候可以去请求数据,记得最后调用refreshFinish()复位
     */
    void onRefresh();

    /**
     * 当加载更多时
     */
    void onLoadMore();
  }

  /**
   * 回调接口,可以通过下面的回调,自定义各种状态下的显示效果
   * 可以根据下拉距离scrollY设计动画效果
   */
  public interface OnPullShowViewListener {

    /**
     * 当处于下拉刷新时,头布局显示效果
     *
     * @param scrollY    下拉的距离
     * @param headviewHeight 头布局高度
     * @param deltaY     moveY-lastMoveY,正值为向下拉
     */
    void onPullDownRefreshState(int scrollY, int headviewHeight, int deltaY);

    /**
     * 当处于松手刷新时,头布局显示效果
     *
     * @param scrollY 下拉的距离
     * @param deltaY moveY-lastMoveY,正值为向下拉
     */
    void onReleaseRefreshState(int scrollY, int deltaY);

    /**
     * 正在刷新时,页面的显示效果
     */
    void onRefreshingState();

    /**
     * 默认状态时,页面显示效果,主要是为了复位各种状态
     */
    void onDefaultState();

  }
}

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

(0)

相关推荐

  • Android RecyclerView实现水平、垂直方向分割线

    android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现RecyclerView的水平/垂直分割线,则需要继承自RecyclerView.ItemDecoration重写getItemOffsets方法,从而增加水平/垂直分割线. 写一个例子. MainActivity.Java: package zhangphil.app; import android.content.Context; import android.graphics.Col

  • Android利用RecyclerView编写聊天界面

    本文实例为大家分享了Android RecyclerView编写聊天界面的具体代码,供大家参考,具体内容如下 1.待会儿会用到RecyclerView,首先在app/build.gradle(注意有两个build.gradle,选择app下的那个)当中添加依赖库,如下: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:24.2.1'

  • Android中RecyclerView实现横向滑动代码

    RecyclerView 是Android L版本中新添加的一个用来取代ListView的SDK,它的灵活性与可替代性比listview更好.本文给大家介绍Android中RecyclerView实现横向滑动代码,一起看看吧. android.support.v7.widget.RecyclerView 功能:RecyclerView横向滑动 控件:<android.support.v7.widget.RecyclerView /> Java类:RecyclerView.GalleryAdap

  • Android RecyclerView实现数据列表展示效果

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 1.首先导入依赖: compile 'com.android.support:recyclerview-v7:25.3.1' 2.适配器类: public class NewsAdapter extends RecyclerView.Adapter { private View view; private Context mcontext; private ArrayList<NewsData.DataBean

  • Android RecyclerView显示Item布局不一致解决办法

    RecyclerView显示Item布局不一致 在自定义RecyclerAdapter的时候,在重写onCreateViewHolder方法是使用了 @Override public H onCreateViewHolder(ViewGroup parent, int viewType) { View view=View.inflate(context,layoutId,null); return view; } 进行生成布局,结果发现生成的布局没有LayoutParams.以前自定义View的

  • Android中使用RecyclerView实现下拉刷新和上拉加载

    推荐阅读:使用RecyclerView添加Header和Footer的方法                       RecyclerView的使用之HelloWorld RecyclerView 是Android L版本中新添加的一个用来取代ListView的SDK,它的灵活性与可替代性比listview更好.本文给大家介绍如何为RecyclerView添加下拉刷新和上拉加载,过去在ListView当中添加下拉刷新和上拉加载是非常方便的利用addHeaderView和addFooterVie

  • Android RecyclerView使用方法详解

    本文为大家分享了Android RecyclerView使用方法,供大家参考,具体内容如下 1.RecyclerView 是在Android support - v7 里面提供的 新的列表组件,用来替代传统的ListView. . 要使用RecyclerView 需要给我工程添加 support:recycle-v7 的支持: app 右键 - Open Module Settings - Dependencies(依赖项) - 点 + 号 - 添加一个库 upport:recycle-v7 

  • Android中RecyclerView布局代替GridView实现类似支付宝的界面

    单纯使用GridView 通用的两种给GridView 添加分割线的方法:http://stackoverflow.com/questions/7132030/android-gridview-draw-dividers 给Gridview 添加分割线,也就是实现网格布局,不清楚谷歌为什么没有给Gridview 添加一个类似 ListView 的Divider 属性,因此就需要我们自己去添加分割线, 目前两种方法,第一种是 利用GridView 的  android:horizontalSpac

  • Android使用RecyclerView实现今日头条频道管理功能

    使用过今日头条的伙计们对这个效果肯定很熟悉.拖拽可排序,点击标签后可以删除.今天我们采用RecyclerView来实现. 实现思路: 通过ItemTouchHelper来绑定RecyclerView的子控件触摸事件. 当滑动拖拽的时候,通知适配器来交换两个子控件的显示位置. 更改数据源,使数据源与子空间显示内容一致. 这就是实现的基本思路,是不是很简单?当然,首先要了解一下ItemTouchHelper这哥们儿是干啥的,有什么作用. This is a utility class to add

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

    RecyclerView已经出来很久了,许许多多的项目都开始从ListView转战RecyclerView,那么,上拉加载和下拉刷新是一件很有必要的事情. 在ListView上,我们可以通过自己添加addHeadView和addFootView去添加头布局和底部局实现自定义的上拉和下拉,或者使用一些第三方库来简单的集成,例如Android-pulltorefresh或者android-Ultra-Pull-to-Refresh,后者的自定义更强,但需要自己实现上拉加载. 而在下面我们将用两种方式

随机推荐