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

switch (ev.getAction()) {
  case MotionEvent.ACTION_DOWN:
   mLastY = ev.getRawY();
   break;
  case MotionEvent.ACTION_MOVE:
   final float deltaY = ev.getRawY() - mLastY;
   mLastY = ev.getRawY();
   if (getFirstVisiblePosition() == 0
     && (mHeaderView.getVisiableHeight() > 0 || deltaY > 0)) {
    // the first item is showing, header has shown or pull down.
    updateHeaderHeight(deltaY / OFFSET_RADIO);
    invokeOnScrolling();
   } else if (getLastVisiblePosition() == mTotalItemCount - 1
     && (mFooterView.getBottomMargin() > 0 || deltaY < 0)) {
    // last item, already pulled up or want to pull up.
    updateFooterHeight(-deltaY / OFFSET_RADIO);
   }
   break;
  default:
   mLastY = -1; // reset
   if (getFirstVisiblePosition() == 0) {
    // invoke refresh
    if (mEnablePullRefresh
      && mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
     mPullRefreshing = true;
     mHeaderView.setState(XListViewHeader.STATE_REFRESHING);
     if (mListViewListener != null) {
      mListViewListener.onRefresh();
     }
    }
    resetHeaderHeight();
   } else if (getLastVisiblePosition() == mTotalItemCount - 1) {
    // invoke load more.
    if (mEnablePullLoad
        && mFooterView.getBottomMargin() > PULL_LOAD_MORE_DELTA
        && !mPullLoading) {
     startLoadMore();
    }
    resetFooterHeight();
   }
   break;
  }
  return super.onTouchEvent(ev);
 }

@Override
 public void computeScroll() {
  if (mScroller.computeScrollOffset()) {
   if (mScrollBack == SCROLLBACK_HEADER) {
    mHeaderView.setVisiableHeight(mScroller.getCurrY());
   } else {
    mFooterView.setBottomMargin(mScroller.getCurrY());
   }
   postInvalidate();
   invokeOnScrolling();
  }
  super.computeScroll();
 }

@Override
 public void setOnScrollListener(OnScrollListener l) {
  mScrollListener = l;
 }

@Override
 public void onScrollStateChanged(AbsListView view, int scrollState) {
  if (mScrollListener != null) {
   mScrollListener.onScrollStateChanged(view, scrollState);
  }
 }

@Override
 public void onScroll(AbsListView view, int firstVisibleItem,
   int visibleItemCount, int totalItemCount) {
  // send to user's listener
  mTotalItemCount = totalItemCount;
  if (mScrollListener != null) {
   mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,
     totalItemCount);
  }
 }

public void setXListViewListener(IXListViewListener l) {
  mListViewListener = l;
 }

/**
  * you can listen ListView.OnScrollListener or this one. it will invoke
  * onXScrolling when header/footer scroll back.
  */
 public interface OnXScrollListener extends OnScrollListener {
  public void onXScrolling(View view);
 }

/**
  * implements this interface to get refresh/load more event.
  */
 public interface IXListViewListener {
  public void onRefresh();

public void onLoadMore();
 }
}

XListViewActivity类;主要学习这个类;调用其他的类就ok

package me.maxwin;

import java.util.ArrayList;

import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;

public class XListViewActivity extends Activity implements IXListViewListener {
 private XListView mListView;
 private ArrayAdapter<String> mAdapter;
 private ArrayList<String> items = new ArrayList<String>();
 private Handler mHandler;
 private int start = 0;
 private static int refreshCnt = 0;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  geneItems();
  mListView = (XListView) findViewById(R.id.xListView);
  mListView.setPullLoadEnable(true);
  mAdapter = new ArrayAdapter<String>(this, R.layout.list_item, items);
  mListView.setAdapter(mAdapter);
//  mListView.setPullLoadEnable(false);
//  mListView.setPullRefreshEnable(false);
  mListView.setXListViewListener(this);
  mHandler = new Handler();
 }

private void geneItems() {
  for (int i = 0; i != 20; ++i) {
   items.add("refresh cnt " + (++start));
  }
 }

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

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