Android下拉刷新上拉加载简单的实现方法(4)

/**
  * enable or disable pull up load more feature.
  *
  * @param enable
  */
 public void setPullLoadEnable(boolean enable) {
  mEnablePullLoad = enable;
  if (!mEnablePullLoad) {
   mFooterView.hide();
   mFooterView.setOnClickListener(null);
   //make sure "pull up" don't show a line in bottom when listview with one page
   setFooterDividersEnabled(false);
  } else {
   mPullLoading = false;
   mFooterView.show();
   mFooterView.setState(XListViewFooter.STATE_NORMAL);
   //make sure "pull up" don't show a line in bottom when listview with one page 
   setFooterDividersEnabled(true);
   // both "pull up" and "click" will invoke load more.
   mFooterView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     startLoadMore();
    }
   });
  }
 }

/**
  * stop refresh, reset header view.
  */
 public void stopRefresh() {
  if (mPullRefreshing == true) {
   mPullRefreshing = false;
   resetHeaderHeight();
  }
 }

/**
  * stop load more, reset footer view.
  */
 public void stopLoadMore() {
  if (mPullLoading == true) {
   mPullLoading = false;
   mFooterView.setState(XListViewFooter.STATE_NORMAL);
  }
 }

/**
  * set last refresh time
  *
  * @param time
  */
 public void setRefreshTime(String time) {
  mHeaderTimeView.setText(time);
 }

private void invokeOnScrolling() {
  if (mScrollListener instanceof OnXScrollListener) {
   OnXScrollListener l = (OnXScrollListener) mScrollListener;
   l.onXScrolling(this);
  }
 }

private void updateHeaderHeight(float delta) {
  mHeaderView.setVisiableHeight((int) delta
    + mHeaderView.getVisiableHeight());
  if (mEnablePullRefresh && !mPullRefreshing) { // 鏈浜庡埛鏂扮姸鎬侊紝鏇存柊绠ご
   if (mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
    mHeaderView.setState(XListViewHeader.STATE_READY);
   } else {
    mHeaderView.setState(XListViewHeader.STATE_NORMAL);
   }
  }
  setSelection(0); // scroll to top each time
 }

/**
  * reset header view's height.
  */
 private void resetHeaderHeight() {
  int height = mHeaderView.getVisiableHeight();
  if (height == 0) // not visible.
   return;
  // refreshing and header isn't shown fully. do nothing.
  if (mPullRefreshing && height <= mHeaderViewHeight) {
   return;
  }
  int finalHeight = 0; // default: scroll back to dismiss header.
  // is refreshing, just scroll back to show all the header.
  if (mPullRefreshing && height > mHeaderViewHeight) {
   finalHeight = mHeaderViewHeight;
  }
  mScrollBack = SCROLLBACK_HEADER;
  mScroller.startScroll(0, height, 0, finalHeight - height,
    SCROLL_DURATION);
  // trigger computeScroll
  invalidate();
 }

private void updateFooterHeight(float delta) {
  int height = mFooterView.getBottomMargin() + (int) delta;
  if (mEnablePullLoad && !mPullLoading) {
   if (height > PULL_LOAD_MORE_DELTA) { // height enough to invoke load
             // more.
    mFooterView.setState(XListViewFooter.STATE_READY);
   } else {
    mFooterView.setState(XListViewFooter.STATE_NORMAL);
   }
  }
  mFooterView.setBottomMargin(height);

//  setSelection(mTotalItemCount - 1); // scroll to bottom
 }

private void resetFooterHeight() {
  int bottomMargin = mFooterView.getBottomMargin();
  if (bottomMargin > 0) {
   mScrollBack = SCROLLBACK_FOOTER;
   mScroller.startScroll(0, bottomMargin, 0, -bottomMargin,
     SCROLL_DURATION);
   invalidate();
  }
 }

private void startLoadMore() {
  mPullLoading = true;
  mFooterView.setState(XListViewFooter.STATE_LOADING);
  if (mListViewListener != null) {
   mListViewListener.onLoadMore();
  }
 }

@Override
 public boolean onTouchEvent(MotionEvent ev) {
  if (mLastY == -1) {
   mLastY = ev.getRawY();
  }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7f9af977b63f35b67d5952bd2a6ab86c.html