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

/**
 * @file XListView.java
 * @package me.maxwin.view
 * @create Mar 18, 2012 6:28:41 PM
 * @author Maxwin
 * @description An ListView support (a) Pull down to refresh, (b) Pull up to load more.
 *   Implement IXListViewListener, and see stopRefresh() / stopLoadMore().
 */
package me.maxwin.view;

import me.maxwin.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;

public class XListView extends ListView implements OnScrollListener {

private float mLastY = -1; // save event y
 private Scroller mScroller; // used for scroll back
 private OnScrollListener mScrollListener; // user's scroll listener

// the interface to trigger refresh and load more.
 private IXListViewListener mListViewListener;

// -- header view
 private XListViewHeader mHeaderView;
 // header view content, use it to calculate the Header's height. And hide it
 // when disable pull refresh.
 private RelativeLayout mHeaderViewContent;
 private TextView mHeaderTimeView;
 private int mHeaderViewHeight; // header view's height
 private boolean mEnablePullRefresh = true;
 private boolean mPullRefreshing = false; // is refreashing.

// -- footer view
 private XListViewFooter mFooterView;
 private boolean mEnablePullLoad;
 private boolean mPullLoading;
 private boolean mIsFooterReady = false;
 
 // total list items, used to detect is at the bottom of listview.
 private int mTotalItemCount;

// for mScroller, scroll back from header or footer.
 private int mScrollBack;
 private final static int SCROLLBACK_HEADER = 0;
 private final static int SCROLLBACK_FOOTER = 1;

private final static int SCROLL_DURATION = 400; // scroll back duration
 private final static int PULL_LOAD_MORE_DELTA = 50; // when pull up >= 50px
              // at bottom, trigger
              // load more.
 private final static float OFFSET_RADIO = 1.8f; // support iOS like pull
             // feature.

/**
  * @param context
  */
 public XListView(Context context) {
  super(context);
  initWithContext(context);
 }

public XListView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initWithContext(context);
 }

public XListView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  initWithContext(context);
 }

private void initWithContext(Context context) {
  mScroller = new Scroller(context, new DecelerateInterpolator());
  // XListView need the scroll event, and it will dispatch the event to
  // user's listener (as a proxy).
  super.setOnScrollListener(this);

// init header view
  mHeaderView = new XListViewHeader(context);
  mHeaderViewContent = (RelativeLayout) mHeaderView
    .findViewById(R.id.xlistview_header_content);
  mHeaderTimeView = (TextView) mHeaderView
    .findViewById(R.id.xlistview_header_time);
  addHeaderView(mHeaderView);

// init footer view
  mFooterView = new XListViewFooter(context);

// init header height
  mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
    new OnGlobalLayoutListener() {
     @Override
     public void onGlobalLayout() {
      mHeaderViewHeight = mHeaderViewContent.getHeight();
      getViewTreeObserver()
        .removeGlobalOnLayoutListener(this);
     }
    });
 }

@Override
 public void setAdapter(ListAdapter adapter) {
  // make sure XListViewFooter is the last footer view, and only add once.
  if (mIsFooterReady == false) {
   mIsFooterReady = true;
   addFooterView(mFooterView);
  }
  super.setAdapter(adapter);
 }

/**
  * enable or disable pull down refresh feature.
  *
  * @param enable
  */
 public void setPullRefreshEnable(boolean enable) {
  mEnablePullRefresh = enable;
  if (!mEnablePullRefresh) { // disable, hide the content
   mHeaderViewContent.setVisibility(View.INVISIBLE);
  } else {
   mHeaderViewContent.setVisibility(View.VISIBLE);
  }
 }

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

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