【Android开发日记】SwipeRefreshLayout添加上拉加载功能

通知:完美代码已有小伙伴更新!

地址:http://blog.csdn.net/u012036813/article/details/38959507


关于下拉刷新:

目前市场上下拉刷新大部分是基于handmark的pulltorefresh这一开源项目,直接导入包即可使用下拉刷新和上拉加载功能。当然也有基于这一项目而自己改写的Viewpager,ScrollView的下拉刷新。

SwipeRefreshLayout是google官方下拉刷新组件,俗称“彩虹条”。但是官方版本只有下拉刷新而没有上拉加载更多,因此我进行了改写的尝试。


如何改写:

1.更新Android SDK中的Android support library 版本到19.1


2.前往Android SDK 的support v4目录..android-sdk\extras\android\support\v4\src\java\android\support\v4\widget\

  下将3个文件拷贝到自己的项目中


3.xml中 以自定义控件即包名引用的方式调用SwipeRefreshLayout

   java文件中继承 SwipeRefreshLayout.OnRefreshListener,SwipeRefreshLayout.OnLoadListener接口,并实现





我的改写结果:

1.SwipeRefreshLayout中仿照OnRefresh添加了OnLoad

2.在布局底部新添加了另一个progressbar

3.新定义了onLoad Listener,在调用文件中实例化onRefresh()和onLoad()

存在的Bug:

1.在上拉操作但是上拉的距离不够未达到加载的高度时,下方progressbar没有加载动画。可参照下拉刷新动画。

2.上拉执行加载动作时,下方progressbar动画异常。可参照下拉刷新动画。


【因为个人学习安排原因没有时间继续修改,现将代码公布,仅供参考,也希望大家能将bug如何改进留言给我,谢谢!】

只修改了SwipeRefreshLayout的代码:

/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ...swiperefresh;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
import android.widget.AbsListView;


/**
 * The SwipeRefreshLayout should be used whenever the user can refresh the
 * contents of a view via a vertical swipe gesture. The activity that
 * instantiates this view should add an OnRefreshListener to be notified
 * whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout
 * will notify the listener each and every time the gesture is completed again;
 * the listener is responsible for correctly determining when to actually
 * initiate a refresh of its content. If the listener determines there should
 * not be a refresh, it must call setRefreshing(false) to cancel any visual
 * indication of a refresh. If an activity wishes to show just the progress
 * animation, it should call setRefreshing(true). To disable the gesture and progress
 * animation, call setEnabled(false) on the view.
 *
 * <p> This layout should be made the parent of the view that will be refreshed as a
 * result of the gesture and can only support one direct child. This view will
 * also be made the target of the 
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值