下拉刷新,其主要思路是,定义一个LinearLayout,自定义view中,控制好touch事件以及需要灵活使用scrollTo函数,让控件进行平滑的滚动,实现平滑的动画效果。
本篇博客,主要介绍 LoadingLayoutProxy 类的使用,对该项目中,使用到的proxy设计模式,进行剖析。
代理模式基本原理:代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。
LoadingLayoutProxy为代理对象,代理的原对象为LoadingLayout的子类,即RotateLoadingLayout或者FlipLoadingLayout。开源项目中,默认Listview列表的下拉&上拉刷新,其实为4个RotateLoadingLayout对象,被添加到窗口中,PullToRefreshListView类中,通过addHeaderView和addFooterView添加到listview中。
推荐此篇博客,http://blog.csdn.net/birdsaction/article/details/44831737#comments,已经将view的布局,描述得比较清楚。
public class LoadingLayoutProxy implements ILoadingLayout {
private final HashSet<LoadingLayout> mLoadingLayouts;
LoadingLayoutProxy() {
mLoadingLayouts = new HashSet<LoadingLayout>();
}
/**
* This allows you to add extra LoadingLayout instances to this proxy. This
* is only necessary if you keep your own instances, and want to have them
* included in any
* {@link PullToRefreshBase#createLoadingLayoutProxy(boolean, boolean)
* createLoadingLayoutProxy(...)} calls.
*
* @param layout - LoadingLayout to have included.
*/
public void addLayout(LoadingLayout layout) {
if (null != layout) {
mLoadingLayouts.add(layout);
}
}
... ... ... ...
代理原对象,即4个RotateLoadingLayout布局对象,通过addLayout添加到HashSet的mLoadingLayouts中。
这里,可以简单看看proxy类的定义:
PullToRefreshListView.java中的方法:
@Override
protected LoadingLayoutProxy createLoadingLayoutProxy(final boolean includeStart, final boolean includeEnd) {
/*
* super.createLoadingLayoutProx调用父类PullToRefreshBase,
* 将两个RotateLoadingLayout分别添加到HashSet<LoadingLayout>对象中
* */
LoadingLayoutProxy proxy = super.createLoadingLayoutProxy(includeStart, includeEnd);
if (mListViewExtrasEnabled) {
final Mode mode = getMode();
/*然后将两个RotateLoadingLayout对象(listview中header和footer),添加到mLoadingLayouts中
*至此,四个RotateLoadingLayout均添加到HashSet对象中*/
if (includeStart && mode.showHeaderLoadingLayout()) {
proxy.addLayout(mHeaderLoadingView);
}
if (includeEnd && mode.showFooterLoadingLayout()) {
proxy.addLayout(mFooterLoadingView);
}
}
return proxy;
}
代理模式实现完了,看看是如何使用这个代理对象,其实也很简单,即在PullToRefreshListActivity.java的初始化时,为RotateLoadingLayout的下拉&上拉时,设置的文字
内容,即界面效果如下:
下拉时,显示效果
下拉后,松开后的效果
其中的更新时间:
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
使用proxy类对象,为layout对象中的TextView赋值。