Android-PullToRefresh 进入自动刷新 终极解决方法

最近打算自己写一个框架,于是就用volley+Android-PullToRefresh  ,但是使用Android-PullToRefresh  想实现进入页面自动实现下拉刷新 mPullRefreshListView.setRefreshing(true);  一直没有效果,在网上看了看 ,很多人说view 还没有生成,所以没有效果,才去延时下拉就有效果

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                mPullRefreshListView.setRefreshing();
            }
        }, 3000);

添加此代码后发现,时间很难控制,效果很差。

故自己研究一下:

	@Override
	public final void setRefreshing(boolean doScroll) {
		if (!isRefreshing()) {
			setState(State.MANUAL_REFRESHING, doScroll);
		}
	}

	final void setState(State state, final boolean... params) {
		mState = state;
		if (DEBUG) {
			Log.d(LOG_TAG, "State: " + mState.name());
		}

		switch (mState) {
			case RESET:
				onReset();
				break;
			case PULL_TO_REFRESH:
				onPullToRefresh();
				break;
			case RELEASE_TO_REFRESH:
				onReleaseToRefresh();
				break;
			case REFRESHING:
			case MANUAL_REFRESHING:
				onRefreshing(params[0]);
				break;
			case OVERSCROLLING:
				// NO-OP
				break;
		}

		// Call OnPullEventListener
		if (null != mOnPullEventListener) {
			mOnPullEventListener.onPullEvent(this, mState, mCurrentMode);
		}
	}

	protected void onRefreshing(final boolean doScroll) {
		if (mMode.showHeaderLoadingLayout()) {
			mHeaderLayout.refreshing();
		}
		if (mMode.showFooterLoadingLayout()) {
			mFooterLayout.refreshing();
		}

		if (doScroll) {
			if (mShowViewWhileRefreshing) {

				// Call Refresh Listener when the Scroll has finished
				OnSmoothScrollFinishedListener listener = new OnSmoothScrollFinishedListener() {
					@Override
					public void onSmoothScrollFinished() {
						callRefreshListener();
					}
				};

				switch (mCurrentMode) {
					case MANUAL_REFRESH_ONLY:
					case PULL_FROM_END:
						smoothScrollTo(getFooterSize(), listener);
						break;
					default:
					case PULL_FROM_START:
						smoothScrollTo(-getHeaderSize(),listener);
						break;
				}
			} else {
				smoothScrollTo(0);
			}
		} else {
			// We're not scrolling, so just call Refresh Listener now
			callRefreshListener();
		}
	}



断点后发现,刚进入页面是View 未生成 ,-getHeaderSize()==0 所以没有下滑, 自然而然,只要-给etHeaderSize()正确的值,下拉刷新自然可以完成,所以添加此逻辑

smoothScrollTo((-getHeaderSize()==0)?(-loadingSize):(-getHeaderSize()), listener);   即可,loadingSize自己算即可

你可能感兴趣的:(Android)