PullToRefresh之代理模式-LoadingLayoutProxy源码剖析

        Android平台PullToRefresh开源项目,被很多app使用,其重要性不言而喻了。

下拉刷新,其主要思路是,定义一个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赋值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值