package com.quanquanle.view.pullrefresh;
import com.quanquanle.view.pullrefresh.ILoadingLayout.State;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
/**
* 这个实现了下拉刷新和上拉加载更多的功能
*
* @author Li Hong
* @since 2013-7-29
* @param <T>
*/
@SuppressLint("NewApi")
public abstract class PullToRefreshBase<T extends View> extends LinearLayout implements IPullToRefresh<T> {
/**
* 定义了下拉刷新和上拉加载更多的接口。
*
* @author Li Hong
* @since 2013-7-29
*/
public interface OnRefreshListener<V extends View> {
/**
* 下拉松手后会被调用
*
* @param refreshView 刷新的View
*/
void onPullDownToRefresh(final PullToRefreshBase<V> refreshView);
/**
* 加载更多时会被调用或上拉时调用
*
* @param refreshView 刷新的View
*/
void onPullUpToRefresh(final PullToRefreshBase<V> refreshView);
}
/**回滚的时间*/
private static final int SCROLL_DURATION = 150;
/**阻尼系数*/
private static final float OFFSET_RADIO = 2.5f;
/**上一次移动的点 */
private float mLastMotionY = -1;
/**下拉刷新和加载更多的监听器 */
private OnRefreshListener<T> mRefreshListener;
/**下拉刷新的布局 */
private LoadingLayout mHeaderLayout;
/**上拉加载更多的布局*/
private LoadingLayout mFooterLayout;
/**HeaderView的高度*/
private int mHeaderHeight;
/**FooterView的高度*/
private int mFooterHeight;
/**下拉刷新是否可用*/
private boolean mPullRefreshEnabled = true;
/**上拉加载是否可用*/
private boolean mPullLoadEnabled = false;
/**判断滑动到底部加载是否可用*/
private boolean mScrollLoadEnabled = false;
/**是否截断touch事件*/
private boolean mInterceptEventEnable = true;
/**表示是否消费了touch事件,如果是,则不调用父类的onTouchEvent方法*/
private boolean mIsHandledTouchEvent = false;
/**移动点的保护范围值*/
private int mTouchSlop;
/**下拉的状态*/
private State mPullDownState = State.NONE;
/**上拉的状态*/
private State mPullUpState = State.NONE;
/**可以下拉刷新的View*/
T mRefreshableView;
/**平滑滚动的Runnable*/
private SmoothScrollRunnable mSmoothScrollRunnable;
/**可刷新View的包装布局*/
private FrameLayout mRefreshableViewWrapper;
/**
* 构造方法
*
* @param context context
*/
public PullToRefreshBase(Context context) {
super(context);
init(context, null);
}
/**
* 构造方法
*
* @param context context
* @param attrs attrs
*/
public PullToRefreshBase(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
/**
* 构造方法
*
* @param context context
* @param attrs attrs
* @param defStyle defStyle
*/
public PullToRefreshBase(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
/**
* 初始化
*
* @param context context
*/
private void init(Context context, AttributeSet attrs) {
setOrientation(LinearLayout.VERTICAL);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mHeaderLayout = createHeaderLoadingLayout(context, attrs);
mFooterLayout = createFooterLoadingLayout(context, attrs);
mRefreshableView = createRefreshableView(context, attrs);
if (null == mRefreshableView) {
throw new NullPointerException("Refreshable view can not be null.");
}
addRefreshableView(context, mRefreshableView);
addHeaderAndFooter(context);
// 得到Header的高度,这个高度需要用这种方式得到,在onLayout方法里面得到的高度始终是0
getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
refreshLoadingViewsSize();
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
/**
* 初始化padding,我们根据header和footer的高度来设置top padding和bottom padding
*/
private void refreshLoadingViewsSize() {
// 得到header和footer的内容高度,它将会作为拖动刷新的一个临界值,如果拖动距离大于这个高度
// 然后再松开手,就会触发刷新操作
int headerHeight = (null != mHeaderLayout) ? mHeaderLayout.getContentSize() : 0;
int footerHeight = (null != mFooterLayout) ? mFooterLayout.getContentSize() : 0;
if (headerHeight < 0) {
headerHeight = 0;
}
if (footerHeight < 0) {
footerHeight = 0;
}
mHeaderHeight = headerHeight;
mFooterHeight = footerHeight;
// 这里得到Header和Footer的高度,设置的padding的top和bottom就应该是header和footer的高度
// 因为header和footer是完全看不见的
headerHeight = (null != mHeaderLayout) ? mHeaderLayout.getMeasuredHeight() : 0;
footerHeight = (null != mFooterLayout) ? mFooterLayout.getMeasuredHeight() : 0;
if (0 == footerHeight) {
footerHeight = mFooterHeight;
}
int pLeft = getPaddingLeft();
int pTop = getPaddingTop();
int pRight = getPaddingRight();
int pBottom = getPaddingBottom();
pTop = -headerHeight;
pBottom = -footerHeight;
setPadding(pLeft, pTop, pRight, pBottom);
}
@Override
protected final void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// We need to update the header/footer when our size changes
refreshLoadingViewsSize();
// 设置刷新View的大小
refreshRefreshableViewSize(w, h);
/**
* As we're currently in a Layout Pass, we need to schedule another one
* to layout any changes we've made here
*/
post(new Runnable() {
@Override
public void run() {
requestLayout();
}
});
}
@Override
public void setOrientation(int orientation) {
if (LinearLayout.VERTICAL != orientation) {
throw new IllegalArgumentException("This class only supports VERTICAL orientation.");
}
// Only support vertical orientation
super.setOrientation(orientation);
}
@Override
public final boolean onInterceptTouchEvent(MotionEvent event) {
if (!isInterceptTouchEventEnabled()) {
return false;
}
if (!isPullLoadEnabled() && !isPullRefreshEnabled()) {
return false;
}
final int action = event.getAction();
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
mIsHandledTouchEvent = false;
return false;
}
if (action != MotionEvent.ACTION_DOWN && mIsHandledTouchEvent) {
return true;
}
switch (action) {
case Moti
没有合适的资源?快使用搜索试试~ 我知道了~
Android 封装了ListView及各种View的下拉刷新和上拉加载
共11个文件
java:11个
4星 · 超过85%的资源 需积分: 9 30 下载量 100 浏览量
2014-10-19
23:32:28
上传
评论
收藏 19KB RAR 举报
温馨提示
Android 封装了ListView及各种View的下拉刷新和上拉加载
资源推荐
资源详情
资源评论
收起资源包目录
pullrefresh.rar (11个子文件)
pullrefresh
HeaderLoadingLayout.java 5KB
FooterLoadingLayout.java 3KB
PullToRefreshWebView.java 2KB
PullToRefreshGridView.java 7KB
PullToRefreshScrollView.java 2KB
IPullToRefresh.java 2KB
PullToRefreshListView.java 8KB
LoadingLayout.java 6KB
PullToRefreshBase.java 29KB
RotateLoadingLayout.java 5KB
ILoadingLayout.java 2KB
共 11 条
- 1
资源评论
- nui20002015-01-20没有布局文件、资源文件,只有java文件,下载看需求。还是谢谢侬。
- 2个橘子2015-03-05没有布局文件、资源文件,只有java文件,下载看需求。
flooodsky
- 粉丝: 11
- 资源: 59
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功