怎样实现刷新动画swiperefreshhLayout的自动刷新

官方的下拉刷新出来也挺久了,效果也是比较炫酷,有比较多的应用都已经能看到它的身影了,关于SwipeRefreshLayout大家可以自己去官方查看
我们下面来讲一下它的使用
因为它是依赖包中的,所以首先,我们需要把它引入到我们的工程中
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
然后我们在布局里面使用它,让它里面包裹一个listview
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="@+id/swp"
android:layout_height="match_parent"&
android:id="@+id/lv_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" /&
SwipeRefreshLayout 主要有三个比较常用的方法,分别是
setColorSchemeResources(int… colorResIds)
Set the color resources used in the progress animation from color resources.就是设置那个刷新的圈圈里面那个线的颜色.可以看到它接受的是一个可变参数,最多能设置4个颜色.
setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)
Set the listener to be notified when a refresh is triggered via the swipe gesture.设置监听,用于监听刷新时间的触发
setRefreshing(boolean refreshing)
Notify the widget that refresh state has changed.通知刷新状态的改变,我们可以通过这个方法来设置圈圈的显示和消失
常用的三个方法介绍完了,我们来使用一下
public class SwipeRefreshActivity extends AppCompatActivity
implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout mSwipeRefreshL
private ListView mListV
private List&String& mDatas = new ArrayList&&();
private ContentAdapter mA
private int mI
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_refresh);
initData();
addEvent();
private void addEvent() {
mSwipeRefreshLayout.setOnRefreshListener(this);
private void initUI() {
mListView = (ListView) findViewById(R.id.lv_refresh);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swp);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
android.R.color.holo_red_light, android.R.color.holo_orange_light,
android.R.color.holo_green_light);
private void initData() {
generateData();
mAdapter = new ContentAdapter(mDatas, this);
mListView.setAdapter(mAdapter);
* 生成listview的数据
private void generateData() {
for (int i = 0; i & 20; i++) {
mDatas.add("数据" + mIndex++);
* 刷新的监听,这里模拟一个耗时的操作
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
public void run() {
mDatas.clear();
generateData();
mAdapter.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
再贴上adapter的代码
public class ContentAdapter extends BaseAdapter {
private List&String& mD
private Context mC
public ContentAdapter(List&String& mDatas, Context mContext) {
this.mDatas = mD
this.mContext = mC
public int getCount() {
return mDatas == null ? 0 : mDatas.size();
public Object getItem(int i) {
return null;
public long getItemId(int i) {
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewH
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.item_test, viewGroup, false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
viewHolder = (ViewHolder) view.getTag();
viewHolder.mTv.setText(mDatas.get(i));
private static class ViewHolder {
TextView mTv;
public ViewHolder(View view) {
mTv = (TextView) view.findViewById(R.id.tv);
我们来运行一下看看
嗯,效果还是不错的,但是在我们往下拉的时候,我们的那个listview在下拉的时候是没有反应的,那么问题来了,我们需要怎么样去实现下拉自动加载更多了,网上也有比较多教程,有种方式是重写SwipeRefreshLayout来实现的,不过这里我们不用那种方式,我们通过重写Listview ,在listview滑动到底部去监听不也可以实现吗.
原理很简单,就是给他添加footerview,在滑动到底部的时候回调一个方法,让我们来做加载更多的逻辑,然后在加载完去notify一下,在没有数据的时候把footerview remove掉
* Class description goes here.
public class AutoLoadListView extends ListView {
private boolean isL
private LoadingView loadingV
public boolean hasMoreI
private Pagingable pagingableL
public AutoLoadListView(Context context) {
this(context, null);
public AutoLoadListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
public AutoLoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
public boolean isLoading() {
return this.isL
public void setIsLoading(boolean isLoading) {
this.isLoading = isL
public void setPagingableListener(Pagingable pagingableListener) {
this.pagingableListener = pagingableL
public void setHasMoreItems(boolean hasMoreItems) {
this.hasMoreItems = hasMoreI
if (!this.hasMoreItems) {
removeFooterView(loadingView);
} else if (findViewById(R.id.loading_view) == null) {
addFooterView(loadingView);
ListAdapter adapter = ((HeaderViewListAdapter) getAdapter()).getWrappedAdapter();
setAdapter(adapter);
public boolean hasMoreItems() {
return this.hasMoreI
private void init() {
isLoading = false;
loadingView = new LoadingView(getContext());
addFooterView(loadingView);
super.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView absListView, int i) {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int lastVisibleItem = firstVisibleItem + visibleItemC
if (!isLoading && hasMoreItems && (lastVisibleItem == totalItemCount)) {
if (pagingableListener != null) {
isLoading = true;
pagingableListener.onLoadMoreItems();
public interface Pagingable {
void onLoadMoreItems();
说道这里 ,相信细心的同学已经看到了醒目的类注释,咳咳,我们来使用一下
public class AutoLoadRegreshActivity extends AppCompatActivity
implements AutoLoadListView.Pagingable, SwipeRefreshLayout.OnRefreshListener {
private AutoLoadListView mListV
private List&String& mDatas = new ArrayList&&();
private ContentAdapter mA
private SwipeRefreshLayout mSwipeRefreshL
private int mI
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initData();
addEvent();
private void initUI() {
mListView = (AutoLoadListView) findViewById(R.id.paging_list_view);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swp);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
android.R.color.holo_red_light, android.R.color.holo_orange_light,
android.R.color.holo_green_light);
private void initData() {
generateData();
mAdapter = new ContentAdapter(mDatas,this);
mListView.setAdapter(mAdapter);
mListView.setHasMoreItems(true);
* 生成数据
private void generateData() {
for (int i = 0; i & 20; i++) {
mDatas.add("数据" + mIndex ++);
private void addEvent() {
mListView.setPagingableListener(this);
mSwipeRefreshLayout.setOnRefreshListener(this);
* 加载更多数据
public void onLoadMoreItems() {
new Handler().postDelayed(new Runnable() {
public void run() {
generateData();
mAdapter.notifyDataSetChanged();
mListView.setIsLoading(false);
* 刷新数据,把集合清空,把角标设置为0
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
public void run() {
mDatas.clear();
mIndex = 0;
generateData();
mAdapter.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
我们来看看效果
到这里基本的功能都完成了,不过就是加载哏多的样式有点单一,我们来自己定义一下,找到LoadingView
public class LoadingView extends LinearLayout {
public LoadingView(Context context) {
super(context);
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
private void init() {
inflate(getContext(), R.layout.loading_view, this);
我们再去打开布局文件
&LinearLayout android:id="@+id/loading_view"
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/loading_view_margin_layout"
android:gravity="center"
android:orientation="horizontal"&
&ProgressBar
android:id="@+id/video_item_image"
style="?android:progressBarStyle"
android:layout_width="@dimen/loading_view_progress_size"
android:layout_height="@dimen/loading_view_progress_size"
android:layout_marginRight="@dimen/loading_view_margin_right"
android:indeterminate="true"/&
android:id="@+id/video_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"/&
&/LinearLayout&
好的,我们已经找到了加载更多的布局文件,直接根据自己需要修改一下就完成了,我自己也自己定义了一个,代码就不贴了,就看下效果把
好了,到这里我们的功能就已经完成了.
项目源码在github上面
加载更多是参照开源项目,大家可以自己去看看,
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2819次
排名:千里之外
(1)(1)(1)(1)(1)(1)Android SwipeRefreshLayout 自动刷新 - 简书
<div class="fixed-btn note-fixed-download" data-toggle="popover" data-placement="left" data-html="true" data-trigger="hover" data-content=''>
写了28459字,被20人关注,获得了60个喜欢
Android SwipeRefreshLayout 自动刷新
调用Android的SwipeRefreshLayout 的setRefreshing() 方法,本以为可以完成像知乎客户端的自动刷新,可是并没有,在网上找了一下问题的所在
Paste_Image.png
这里的mOriginalOffsetTop参数是在onMeasure方法中获取的,在onMeasure没有调用的时候mOriginalOffsetTop是没有值得,所以按照这个思路去处理这个问题。既然onMeasure没有执行,我们就保证让onMeasure执行之后再去setRefreshing()
所以代码可以这样写
public class SwipeRefresh extends SwipeRefreshLayout
private boolean mMeasured =
private boolean mRefresh =
public SwipeRefresh(Context context)
super(context);
public SwipeRefresh(Context context, AttributeSet attrs)
super(context, attrs);
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!mMeasured) {
mMeasured =
setRefreshing(mRefresh)
public void setRefreshing(boolean refreshing)
if (mMeasured) {
super.setRefreshing();
mRefresh =
这样, 问题就解决了
XRecyclerView 与 SwipeRefreshLayout一起用会有坑, XRecyclerView自己实现了上下拉的监听,把XRecyclerView做为SwipeRefreshLayout的子view,SwipeRefreshLayout就判断不了下拉事件了,从这个问题上卡主好久。提供一个CoordinatorLayout 的动画,像钉钉上面的titleBar一样的动画
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:经检测你所在的网络可能存在爬虫,因资源限制,我们只能拒绝你的请求。
如果你是推酷的用户,可以以继续访问使用。
如有疑问,可将IP信息发送到
请求解封。

我要回帖

更多关于 swiperefreshlayout包 的文章

 

随机推荐