使用PullToRefresh区分下拉刷新和上拉加载

关于怎么使用PullToRefresh,网上已经有很多的文章了,在这里就不赘述了。

在使用PullToRefresh这个库的时候需要区分是上拉还是下拉,百度一下就能得到一个修改PullToRefresh.java源文件的方法,如http://blog.csdn.net/losetowin/article/details/18261389该博文中提到的方法。

其实PullToRefresh已经给我们提供了方法,查看源码。

com.handmark.pulltorefresh.library.PullToRefreshBase.java里面设置刷新监听的一段代码:

	@Override
	public final void setOnRefreshListener(OnRefreshListener<T> listener) {
		mOnRefreshListener = listener;
		mOnRefreshListener2 = null;
	}

	@Override
	public final void setOnRefreshListener(OnRefreshListener2<T> listener) {
		mOnRefreshListener2 = listener;
		mOnRefreshListener = null;
	}

再看com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2<T>

<span style="white-space:pre">	</span>/**
	 * An advanced version of the Listener to listen for callbacks to Refresh.
	 * This listener is different as it allows you to differentiate between Pull
	 * Ups, and Pull Downs.
	 * 
	 * @author Chris Banes
	 */
	public static interface OnRefreshListener2<V extends View> {
		// TODO These methods need renaming to START/END rather than DOWN/UP

		/**
		 * onPullDownToRefresh will be called only when the user has Pulled from
		 * the start, and released.
		 */
		public void onPullDownToRefresh(final PullToRefreshBase<V> refreshView);

		/**
		 * onPullUpToRefresh will be called only when the user has Pulled from
		 * the end, and released.
		 */
		public void onPullUpToRefresh(final PullToRefreshBase<V> refreshView);

	}
所以在使用的时候可以:

mPullRefreshListView.setOnRefreshListener(new  com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2<ListView>() {
	@Override
	public void onPullDownToRefresh(
		PullToRefreshBase<ListView> refreshView) {
		Toast.makeText(PullToRefreshListActivity.this, "onPullDownToRefresh", Toast.LENGTH_SHORT).show();
		new GetDataTask().execute();
	}
	@Override
	public void onPullUpToRefresh(
		PullToRefreshBase<ListView> refreshView) {
		Toast.makeText(PullToRefreshListActivity.this, "onPullUpToRefresh", Toast.LENGTH_SHORT).show();
		new GetDataTask().execute();
	}
});


你可能感兴趣的:(下拉刷新,上拉加载,pulltorefresh,区分上拉下拉)