android UI進階之實現listview的下拉加載

關於listview的操作五花八門,有下拉刷新,分級顯示,分頁列表,逐頁加載等,以後會陸續和大傢分享這些技術,今天講下下拉加載這個功能的實現。

最初的下拉加載應該是ios上的效果,現在很多應用如新浪微博等都加入瞭這個操作。即下拉listview刷新列表,這無疑是一個非常友好的操作。今天就和大傢分享下這個操作的實現。

先看下運行效果:

 

 

代碼參考國外朋友Johan Nilsson的實現,http://johannilsson.com/2011/03/13/android-pull-to-refresh-update.html。

主要原理為監聽觸摸和滑動操作,在listview頭部加載一個視圖。那要做的其實很簡單:1.寫好加載到listview頭部的view 2.重寫listview,實現onTouchEvent方法和onScroll方法,監聽滑動狀態。計算headview全部顯示出來即可實行加載動作,加載完成即刷新列表。重新隱藏headview。

首先寫下headview的xml代碼:

[html] <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingTop="10dip" 
    android:paddingBottom="15dip" 
    android:gravity="center" 
        android:id="@+id/pull_to_refresh_header" 
    > 
    <ProgressBar  
        android:id="@+id/pull_to_refresh_progress" 
        android:indeterminate="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="30dip" 
        android:layout_marginRight="20dip" 
        android:layout_marginTop="10dip" 
        android:visibility="gone" 
        android:layout_centerVertical="true" 
        style="?android:attr/progressBarStyleSmall" 
        /> 
    <ImageView 
        android:id="@+id/pull_to_refresh_image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="30dip" 
        android:layout_marginRight="20dip" 
        android:visibility="gone" 
        android:layout_gravity="center" 
        android:gravity="center" 
        android:src="@drawable/ic_pulltorefresh_arrow" 
        /> 
    <TextView 
        android:id="@+id/pull_to_refresh_text" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:textStyle="bold" 
        android:paddingTop="5dip" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:gravity="center" 
        /> 
    <TextView 
        android:id="@+id/pull_to_refresh_updated_at" 
        android:layout_below="@+id/pull_to_refresh_text" 
        android:visibility="gone" 
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:gravity="center" 
        /> 
</RelativeLayout> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="10dip"
    android:paddingBottom="15dip"
    android:gravity="center"
        android:id="@+id/pull_to_refresh_header"
    >
    <ProgressBar
        android:id="@+id/pull_to_refresh_progress"
        android:indeterminate="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="10dip"
        android:visibility="gone"
        android:layout_centerVertical="true"
        style="?android:attr/progressBarStyleSmall"
        />
    <ImageView
        android:id="@+id/pull_to_refresh_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="20dip"
        android:visibility="gone"
        android:layout_gravity="center"
        android:gravity="center"
        android:src="@drawable/ic_pulltorefresh_arrow"
        />
    <TextView
        android:id="@+id/pull_to_refresh_text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:paddingTop="5dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        />
    <TextView
        android:id="@+id/pull_to_refresh_updated_at"
        android:layout_below="@+id/pull_to_refresh_text"
        android:visibility="gone"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        />
</RelativeLayout>

代碼比較簡單,即headview包括一個進度條一個箭頭和兩段文字(一個顯示加載狀態,另一個顯示最後刷新時間,本例就不設置瞭)。

而後重寫listview,代碼如下:

[java] package com.notice.pullrefresh; 
 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.animation.LinearInterpolator; 
import android.view.animation.RotateAnimation; 
import android.widget.AbsListView; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.ImageView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
 
  
 
public class PullToRefreshListView extends ListView implements OnScrollListener { 
 
    // 狀態  
    private static final int TAP_TO_REFRESH = 1; 
    private static final int PULL_TO_REFRESH = 2; 
    private static final int RELEASE_TO_REFRESH = 3; 
    private static final int REFRESHING = 4; 
 
 
    private OnRefreshListener mOnRefreshListener; 
 
 
    // 監聽對listview的滑動動作  
    private OnScrollListener mOnScrollListener; 
    private LayoutInflater mInflater; 
 
    //頂部刷新時出現的控件  
    private RelativeLayout mRefreshView; 
    private TextView mRefreshViewText; 
    private ImageView mRefreshViewImage; 
    private ProgressBar mRefreshViewProgress; 
    private TextView mRefreshViewLastUpdated; 
 
    // 當前滑動狀態  
    private int mCurrentScrollState; 
    // 當前刷新狀態  
    private int mRefreshState; 
     
    // 箭頭動畫效果  
    private RotateAnimation mFlipAnimation; 
    private RotateAnimation mReverseFlipAnimation; 
 
    private int mRefreshViewHeight; 
    private int mRefreshOriginalTopPadding; 
    private int mLastMotionY; 
 
    private boolean mBounceHack; 
 
    public PullToRefreshListView(Context context) { 
        super(context); 
        init(context); 
    } 
 
    public PullToRefreshListView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        init(context); 
    } 
 
    public PullToRefreshListView(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
        init(context); 
    } 
 
    /**
     * 初始化控件和箭頭動畫(這裡直接在代碼中初始化動畫而不是通過xml)
     */ 
    private void init(Context context) { 
        mFlipAnimation = new RotateAnimation(0, -180, 
                RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
                RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
        mFlipAnimation.setInterpolator(new LinearInterpolator()); 
        mFlipAnimation.setDuration(250); 
        mFlipAnimation.setFillAfter(true); 
        mReverseFlipAnimation = new RotateAnimation(-180, 0, 
                RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
                RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
        mReverseFlipAnimation.setInterpolator(new LinearInterpolator()); 
        mReverseFlipAnimation.setDuration(250); 
        mReverseFlipAnimation.setFillAfter(true); 
 
        mInflater = (LayoutInflater) context.getSystemService( 
                Context.LAYOUT_INFLATER_SERVICE); 
 
        mRefreshView = (RelativeLayout) mInflater.inflate( 
                R.layout.pull_to_refresh_header, this, false); 
        mRefreshViewText = 
            (TextView) mRefreshView.findViewById(R.id.pull_to_refresh_text); 
        mRefreshViewImage = 
            (ImageView) mRefreshView.findViewById(R.id.pull_to_refresh_image); 
        mRefreshViewProgress = 
            (ProgressBar) mRefreshView.findViewById(R.id.pull_to_refresh_progress); 
        mRefreshViewLastUpdated = 
            (TextView) mRefreshView.findViewById(R.id.pull_to_refresh_updated_at); 
 
        mRefreshViewImage.setMinimumHeight(50); 
        mRefreshOriginalTopPadding = mRefreshView.getPaddingTop(); 
 
        mRefreshState = TAP_TO_REFRESH; 
         
        //為listview頭部增加一個view  
        addHeaderView(mRefreshView); 
 
        super.setOnScrollListener(this); 
 
        measureView(mRefreshView); 
        mRefreshViewHeight = mRefreshView.getMeasuredHeight(); 
    } 
 
    @Override 
    protected void onAttachedToWindow() { 
        setSelection(1); 
    } 
 
    @Override 
    public void setAdapter(ListAdapter adapter) { 
        super.setAdapter(adapter); 
 
        setSelection(1); 
    } 
 
    /**
     * 設置滑動監聽器
     * 
     */ 
    @Override 
    public void setOnScrollListener(AbsListView.OnScrollListener l) { 
        mOnScrollListener = l; 
    } 
 
    /**
     * 註冊一個list需要刷新時的回調接口
     * 
     */ 
    public void setOnRefreshListener(OnRefreshListener onRefreshListener) { 
        mOnRefreshListener = onRefreshListener; 
    } 
 
    /**
     * 設置標簽顯示何時最後被刷新
     * 
     * @param lastUpdated
     *            Last updated at.
     */ 
    public void setLastUpdated(CharSequence lastUpdated) { 
        if (lastUpdated != null) { 
            mRefreshViewLastUpdated.setVisibility(View.VISIBLE); 
            mRefreshViewLastUpdated.setText(lastUpdated); 
        } else { 
            mRefreshViewLastUpdated.setVisibility(View.GONE); 
        } 
    } 
 
    // 實現該方法處理觸摸  
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
        final int y = (int) event.getY(); 
        mBounceHack = false; 
 
        switch (event.getAction()) { 
 
            case MotionEvent.ACTION_UP: 
                if (!isVerticalScrollBarEnabled()) { 
                    setVerticalScrollBarEnabled(true); 
                } 
                if (getFirstVisiblePosition() == 0 && mRefreshState != REFRESHING) { 
                // 拖動距離達到刷新需要  
                    if ((mRefreshView.getBottom() >= mRefreshViewHeight 
                            || mRefreshView.getTop() >= 0) 
                            && mRefreshState == RELEASE_TO_REFRESH) { 
                    // 把狀態設置為正在刷新  
                        mRefreshState = REFRESHING; 
                    // 準備刷新  
                        prepareForRefresh(); 
                    // 刷新  
                        onRefresh(); 
                    } else if (mRefreshView.getBottom() < mRefreshViewHeight 
                            || mRefreshView.getTop() <= 0) { 
                    // 中止刷新  
                        resetHeader(); 
                        setSelection(1); 
                    } 
                } 
                break; 
            case MotionEvent.ACTION_DOWN: 
            // 獲得按下y軸位置  
                mLastMotionY = y; 
                break; 
            case MotionEvent.ACTION_MOVE: 
            // 計算邊距  
                applyHeaderPadding(event); 
                break; 
        } 
        return super.onTouchEvent(event); 
    } 
 
    // 獲得header的邊距  
    private void applyHeaderPadding(MotionEvent ev) { 
 
        int pointerCount = ev.getHistorySize(); 
 
        for (int p = 0; p < pointerCount; p++) { 
            if (mRefreshState == RELEASE_TO_REFRESH) { 
                if (isVerticalFadingEdgeEnabled()) { 
                    setVerticalScrollBarEnabled(false); 
                } 
 
                int historicalY = (int) ev.getHistoricalY(p); 
 
                // 計算申請的邊距,除以1.7使得拉動效果更好  
                int topPadding = (int) (((historicalY – mLastMotionY) 
                        – mRefreshViewHeight) / 1.7); 
 
                mRefreshView.setPadding( 
                        mRefreshView.getPaddingLeft(), 
                        topPadding, 
                        mRefreshView.getPaddingRight(), 
                        mRefreshView.getPaddingBottom()); 
            } 
        } 
    } 
 
    /**
     * 將head的邊距重置為初始的數值
     */ 
    private void resetHeaderPadding() { 
        mRefreshView.setPadding( 
                mRefreshView.getPaddingLeft(), 
                mRefreshOriginalTopPadding, 
                mRefreshView.getPaddingRight(), 
                mRefreshView.getPaddingBottom()); 
    } 
 
    /**
     * 重置header為之前的狀態
     */ 
    private void resetHeader() { 
        if (mRefreshState != TAP_TO_REFRESH) { 
            mRefreshState = TAP_TO_REFRESH; 
 
            resetHeaderPadding(); 
 
            // 將刷新圖標換成箭頭  
            mRefreshViewImage.setImageResource(R.drawable.ic_pulltorefresh_arrow); 
            // 清除動畫  
            mRefreshViewImage.clearAnimation(); 
            // 隱藏圖標和進度條  
            mRefreshViewImage.setVisibility(View.GONE); 
            mRefreshViewProgress.setVisibility(View.GONE); 
        } 
    } 
 
    // 估算headview的width和height  
    private void measureView(View child) { 
        ViewGroup.LayoutParams p = child.getLayoutParams(); 
        if (p == null) { 
            p = new ViewGroup.LayoutParams( 
                    ViewGroup.LayoutParams.FILL_PARENT, 
                    ViewGroup.LayoutParams.WRAP_CONTENT); 
        } 
 
        int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 
                0 + 0, p.width); 
        int lpHeight = p.height; 
        int childHeightSpec; 
        if (lpHeight > 0) { 
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); 
        } else { 
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 
        } 
        child.measure(childWidthSpec, childHeightSpec); 
    } 
 
    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, 
            int visibleItemCount, int totalItemCount) { 
 
        // 在refreshview完全可見時,設置文字為松開刷新,同時翻轉箭頭  
        if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL 
                && mRefreshState != REFRESHING) { 
            if (firstVisibleItem == 0) { 
                mRefreshViewImage.setVisibility(View.VISIBLE); 
                if ((mRefreshView.getBottom() >= mRefreshViewHeight + 20 
                        || mRefreshView.getTop() >= 0) 
                        && mRefreshState != RELEASE_TO_REFRESH) { 
                    mRefreshViewText.setText("松開加載…"); 
                    mRefreshViewImage.clearAnimation(); 
                    mRefreshViewImage.startAnimation(mFlipAnimation); 
                    mRefreshState = RELEASE_TO_REFRESH; 
                } else if (mRefreshView.getBottom() < mRefreshViewHeight + 20 
                        && mRefreshState != PULL_TO_REFRESH) { 
                    mRefreshViewText.setText("下拉刷新…"); 
                    if (mRefreshState != TAP_TO_REFRESH) { 
                        mRefreshViewImage.clearAnimation(); 
                        mRefreshViewImage.startAnimation(mReverseFlipAnimation); 
                    } 
                    mRefreshState = PULL_TO_REFRESH; 
                } 
            } else { 
                mRefreshViewImage.setVisibility(View.GONE); 
                resetHeader(); 
            } 
        } else if (mCurrentScrollState == SCROLL_STATE_FLING 
                && firstVisibleItem == 0 
                && mRefreshState != REFRESHING) { 
            setSelection(1); 
            mBounceHack = true; 
        } else if (mBounceHack && mCurrentScrollState == SCROLL_STATE_FLING) { 
            setSelection(1); 
        } 
 
        if (mOnScrollListener != null) { 
            mOnScrollListener.onScroll(view, firstVisibleItem, 
                    visibleItemCount, totalItemCount); 
        } 
    } 
 
    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
        mCurrentScrollState = scrollState; 
 
        if (mCurrentScrollState == SCROLL_STATE_IDLE) { 
            mBounceHack = false; 
        } 
 
        if (mOnScrollListener != null) { 
            mOnScrollListener.onScrollStateChanged(view, scrollState); 
        } 
    } 
 
    public void prepareForRefresh() { 
        resetHeaderPadding();// 恢復header的邊距  
 
        mRefreshViewImage.setVisibility(View.GONE); 
        // 註意加上,否則仍然顯示之前的圖片  
        mRefreshViewImage.setImageDrawable(null); 
        mRefreshViewProgress.setVisibility(View.VISIBLE); 
 
        // 設置文字  
        mRefreshViewText.setText("加載中…"); 
 
        mRefreshState = REFRESHING; 
    } 
 
    public void onRefresh() { 
 
        if (mOnRefreshListener != null) { 
            mOnRefreshListener.onRefresh(); 
        } 
    } 
 
    /**
     * 重置listview為普通的listview,該方法設置最後更新時間
     * 
     * @param lastUpdated
     *            Last updated at.
     */ 
    public void onRefreshComplete(CharSequence lastUpdated) { 
        setLastUpdated(lastUpdated); 
        onRefreshComplete(); 
    } 
 
    /**
     * 重置listview為普通的listview,不設置最後更新時間
     */ 
    public void onRefreshComplete() {         
 
        resetHeader(); 
 
        // 如果refreshview在加載結束後可見,下滑到下一個條目  
        if (mRefreshView.getBottom() > 0) { 
            invalidateViews(); 
            setSelection(1); 
        } 
    } 
 
 
 
    /**
     * 刷新監聽器接口
     */ 
    public interface OnRefreshListener { 
        /**
         * list需要被刷新時調用
         */ 
        public void onRefresh(); 
    } 

package com.notice.pullrefresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

 

public class PullToRefreshListView extends ListView implements OnScrollListener {

 // 狀態
    private static final int TAP_TO_REFRESH = 1;
    private static final int PULL_TO_REFRESH = 2;
    private static final int RELEASE_TO_REFRESH = 3;
    private static final int REFRESHING = 4;

    private OnRefreshListener mOnRefreshListener;

    // 監聽對listview的滑動動作
    private OnScrollListener mOnScrollListener;
    private LayoutInflater mInflater;

    //頂部刷新時出現的控件
    private RelativeLayout mRefreshView;
    private TextView mRefreshViewText;
    private ImageView mRefreshViewImage;
    private ProgressBar mRefreshViewProgress;
    private TextView mRefreshViewLastUpdated;

 // 當前滑動狀態
    private int mCurrentScrollState;
 // 當前刷新狀態
    private int mRefreshState;
   
 // 箭頭動畫效果
    private RotateAnimation mFlipAnimation;
    private RotateAnimation mReverseFlipAnimation;

    private int mRefreshViewHeight;
    private int mRefreshOriginalTopPadding;
    private int mLastMotionY;

 private boolean mBounceHack;

    public PullToRefreshListView(Context context) {
        super(context);
        init(context);
    }

    public PullToRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PullToRefreshListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    /**
     * 初始化控件和箭頭動畫(這裡直接在代碼中初始化動畫而不是通過xml)
     */
    private void init(Context context) {
        mFlipAnimation = new RotateAnimation(0, -180,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        mFlipAnimation.setInterpolator(new LinearInterpolator());
        mFlipAnimation.setDuration(250);
        mFlipAnimation.setFillAfter(true);
        mReverseFlipAnimation = new RotateAnimation(-180, 0,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
        mReverseFlipAnimation.setDuration(250);
        mReverseFlipAnimation.setFillAfter(true);

        mInflater = (LayoutInflater) context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

  mRefreshView = (RelativeLayout) mInflater.inflate(
    R.layout.pull_to_refresh_header, this, false);
        mRefreshViewText =
            (TextView) mRefreshView.findViewById(R.id.pull_to_refresh_text);
        mRefreshViewImage =
            (ImageView) mRefreshView.findViewById(R.id.pull_to_refresh_image);
        mRefreshViewProgress =
            (ProgressBar) mRefreshView.findViewById(R.id.pull_to_refresh_progress);
        mRefreshViewLastUpdated =
            (TextView) mRefreshView.findViewById(R.id.pull_to_refresh_updated_at);

        mRefreshViewImage.setMinimumHeight(50);
        mRefreshOriginalTopPadding = mRefreshView.getPaddingTop();

        mRefreshState = TAP_TO_REFRESH;
       
        //為listview頭部增加一個view
        addHeaderView(mRefreshView);

        super.setOnScrollListener(this);

  measureView(mRefreshView);
        mRefreshViewHeight = mRefreshView.getMeasuredHeight();
    }

    @Override
    protected void onAttachedToWindow() {
        setSelection(1);
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);

        setSelection(1);
    }

    /**
     * 設置滑動監聽器
     *
     */
    @Override
    public void setOnScrollListener(AbsListView.OnScrollListener l) {
        mOnScrollListener = l;
    }

    /**
     * 註冊一個list需要刷新時的回調接口
     *
     */
    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        mOnRefreshListener = onRefreshListener;
    }

    /**
  * 設置標簽顯示何時最後被刷新
  *
  * @param lastUpdated
  *            Last updated at.
  */
    public void setLastUpdated(CharSequence lastUpdated) {
        if (lastUpdated != null) {
            mRefreshViewLastUpdated.setVisibility(View.VISIBLE);
            mRefreshViewLastUpdated.setText(lastUpdated);
        } else {
            mRefreshViewLastUpdated.setVisibility(View.GONE);
        }
    }

 // 實現該方法處理觸摸
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int y = (int) event.getY();
        mBounceHack = false;

        switch (event.getAction()) {

            case MotionEvent.ACTION_UP:
                if (!isVerticalScrollBarEnabled()) {
                    setVerticalScrollBarEnabled(true);
                }
                if (getFirstVisiblePosition() == 0 && mRefreshState != REFRESHING) {
    // 拖動距離達到刷新需要
                    if ((mRefreshView.getBottom() >= mRefreshViewHeight
                            || mRefreshView.getTop() >= 0)
                            && mRefreshState == RELEASE_TO_REFRESH) {
     // 把狀態設置為正在刷新
                        mRefreshState = REFRESHING;
     // 準備刷新
                        prepareForRefresh();
     // 刷新
                        onRefresh();
                    } else if (mRefreshView.getBottom() < mRefreshViewHeight
                            || mRefreshView.getTop() <= 0) {
     // 中止刷新
                        resetHeader();
                        setSelection(1);
                    }
                }
                break;
            case MotionEvent.ACTION_DOWN:
   // 獲得按下y軸位置
                mLastMotionY = y;
                break;
            case MotionEvent.ACTION_MOVE:
   // 計算邊距
                applyHeaderPadding(event);
                break;
        }
        return super.onTouchEvent(event);
    }

 // 獲得header的邊距
    private void applyHeaderPadding(MotionEvent ev) {

        int pointerCount = ev.getHistorySize();

        for (int p = 0; p < pointerCount; p++) {
            if (mRefreshState == RELEASE_TO_REFRESH) {
                if (isVerticalFadingEdgeEnabled()) {
                    setVerticalScrollBarEnabled(false);
                }

                int historicalY = (int) ev.getHistoricalY(p);

    // 計算申請的邊距,除以1.7使得拉動效果更好
                int topPadding = (int) (((historicalY – mLastMotionY)
                        – mRefreshViewHeight) / 1.7);

                mRefreshView.setPadding(
                        mRefreshView.getPaddingLeft(),
                        topPadding,
                        mRefreshView.getPaddingRight(),
                        mRefreshView.getPaddingBottom());
            }
        }
    }

    /**
  * 將head的邊距重置為初始的數值
  */
    private void resetHeaderPadding() {
        mRefreshView.setPadding(
                mRefreshView.getPaddingLeft(),
                mRefreshOriginalTopPadding,
                mRefreshView.getPaddingRight(),
                mRefreshView.getPaddingBottom());
    }

    /**
  * 重置header為之前的狀態
  */
    private void resetHeader() {
        if (mRefreshState != TAP_TO_REFRESH) {
            mRefreshState = TAP_TO_REFRESH;

            resetHeaderPadding();

   // 將刷新圖標換成箭頭
            mRefreshViewImage.setImageResource(R.drawable.ic_pulltorefresh_arrow);
   // 清除動畫
            mRefreshViewImage.clearAnimation();
   // 隱藏圖標和進度條
            mRefreshViewImage.setVisibility(View.GONE);
            mRefreshViewProgress.setVisibility(View.GONE);
        }
    }

 // 估算headview的width和height
    private void measureView(View child) {
        ViewGroup.LayoutParams p = child.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        int childWidthSpec = ViewGroup.getChildMeasureSpec(0,
                0 + 0, p.width);
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {

  // 在refreshview完全可見時,設置文字為松開刷新,同時翻轉箭頭
        if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL
                && mRefreshState != REFRESHING) {
            if (firstVisibleItem == 0) {
                mRefreshViewImage.setVisibility(View.VISIBLE);
                if ((mRefreshView.getBottom() >= mRefreshViewHeight + 20
                        || mRefreshView.getTop() >= 0)
                        && mRefreshState != RELEASE_TO_REFRESH) {
     mRefreshViewText.setText("松開加載…");
                    mRefreshViewImage.clearAnimation();
                    mRefreshViewImage.startAnimation(mFlipAnimation);
                    mRefreshState = RELEASE_TO_REFRESH;
                } else if (mRefreshView.getBottom() < mRefreshViewHeight + 20
                        && mRefreshState != PULL_TO_REFRESH) {
     mRefreshViewText.setText("下拉刷新…");
                    if (mRefreshState != TAP_TO_REFRESH) {
                        mRefreshViewImage.clearAnimation();
                        mRefreshViewImage.startAnimation(mReverseFlipAnimation);
                    }
                    mRefreshState = PULL_TO_REFRESH;
                }
            } else {
                mRefreshViewImage.setVisibility(View.GONE);
                resetHeader();
            }
        } else if (mCurrentScrollState == SCROLL_STATE_FLING
                && firstVisibleItem == 0
                && mRefreshState != REFRESHING) {
            setSelection(1);
   mBounceHack = true;
  } else if (mBounceHack && mCurrentScrollState == SCROLL_STATE_FLING) {
            setSelection(1);
        }

        if (mOnScrollListener != null) {
            mOnScrollListener.onScroll(view, firstVisibleItem,
                    visibleItemCount, totalItemCount);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        mCurrentScrollState = scrollState;

        if (mCurrentScrollState == SCROLL_STATE_IDLE) {
            mBounceHack = false;
        }

        if (mOnScrollListener != null) {
            mOnScrollListener.onScrollStateChanged(view, scrollState);
        }
    }

    public void prepareForRefresh() {
  resetHeaderPadding();// 恢復header的邊距

        mRefreshViewImage.setVisibility(View.GONE);
  // 註意加上,否則仍然顯示之前的圖片
        mRefreshViewImage.setImageDrawable(null);
        mRefreshViewProgress.setVisibility(View.VISIBLE);

  // 設置文字
  mRefreshViewText.setText("加載中…");

        mRefreshState = REFRESHING;
    }

    public void onRefresh() {

        if (mOnRefreshListener != null) {
            mOnRefreshListener.onRefresh();
        }
    }

    /**
  * 重置listview為普通的listview,該方法設置最後更新時間
  *
  * @param lastUpdated
  *            Last updated at.
  */
    public void onRefreshComplete(CharSequence lastUpdated) {
        setLastUpdated(lastUpdated);
        onRefreshComplete();
    }

    /**
  * 重置listview為普通的listview,不設置最後更新時間
  */
    public void onRefreshComplete() {       

        resetHeader();

  // 如果refreshview在加載結束後可見,下滑到下一個條目
        if (mRefreshView.getBottom() > 0) {
            invalidateViews();
            setSelection(1);
        }
    }

 

    /**
  * 刷新監聽器接口
  */
    public interface OnRefreshListener {
        /**
   * list需要被刷新時調用
   */
        public void onRefresh();
    }
}
 

相信我註釋已經寫的比較詳細瞭,主要註意onTouchEvent和onScroll方法,在這裡面計算頭部邊距,從而通過用戶的手勢實現“下拉刷新”到“松開加載”以及“加載”三個狀態的切換。其中還有一系列和header有關的方法,用來設置header的顯示以及取得header的邊距。於此同時,代碼留出瞭接口以供調用。

那麼現在寫一個測試Activity來試驗下效果:

[java] package com.notice.pullrefresh; 
 
import java.util.Arrays; 
import java.util.LinkedList; 
 
import android.app.ListActivity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
 
import com.notice.pullrefresh.PullToRefreshListView.OnRefreshListener; 
 
 
public class PullrefreshActivity extends ListActivity { 
    private LinkedList<String> mListItems; 
    ArrayAdapter<String> adapter; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.pull_to_refresh); 
 
        // list需要刷新時調用  
        ((PullToRefreshListView) getListView()) 
                .setOnRefreshListener(new OnRefreshListener() { 
                    @Override 
                    public void onRefresh() { 
                        // 在這執行後臺工作  
                        new GetDataTask().execute(); 
                    } 
                }); 
 
 
 
        mListItems = new LinkedList<String>(); 
        mListItems.addAll(Arrays.asList(mStrings)); 
 
        adapter = new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, mListItems); 
 
        setListAdapter(adapter); 
    } 
 
 
    private class GetDataTask extends AsyncTask<Void, Void, String[]> { 
 
        @Override 
        protected String[] doInBackground(Void… params) { 
            // 在這裡可以做一些後臺工作  
            try { 
                Thread.sleep(2000); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            return mStrings; 
        } 
 
        @Override 
        protected void onPostExecute(String[] result) { 
            // 下拉後增加的內容  
            mListItems.addFirst("Added after refresh…"); 
 
            // 刷新完成調用該方法復位  
            ((PullToRefreshListView) getListView()).onRefreshComplete(); 
 
            super.onPostExecute(result); 
        } 
    } 
 
    private String[] mStrings = { "normal data1", "normal data2", 
            "nomal data3", "normal data4", "norma data5", "normal data6" }; 

package com.notice.pullrefresh;

import java.util.Arrays;
import java.util.LinkedList;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import com.notice.pullrefresh.PullToRefreshListView.OnRefreshListener;

public class PullrefreshActivity extends ListActivity {
 private LinkedList<String> mListItems;
 ArrayAdapter<String> adapter;

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  setContentView(R.layout.pull_to_refresh);

  // list需要刷新時調用
  ((PullToRefreshListView) getListView())
    .setOnRefreshListener(new OnRefreshListener() {
     @Override
     public void onRefresh() {
      // 在這執行後臺工作
      new GetDataTask().execute();
     }
    });

 

  mListItems = new LinkedList<String>();
  mListItems.addAll(Arrays.asList(mStrings));

  adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, mListItems);

  setListAdapter(adapter);
 }

 private class GetDataTask extends AsyncTask<Void, Void, String[]> {

  @Override
  protected String[] doInBackground(Void… params) {
   // 在這裡可以做一些後臺工作
   try {
    Thread.sleep(2000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   return mStrings;
  }

  @Override
  protected void onPostExecute(String[] result) {
   // 下拉後增加的內容
   mListItems.addFirst("Added after refresh…");

   // 刷新完成調用該方法復位
   ((PullToRefreshListView) getListView()).onRefreshComplete();

   super.onPostExecute(result);
  }
 }

 private String[] mStrings = { "normal data1", "normal data2",
   "nomal data3", "normal data4", "norma data5", "normal data6" };
}
 

代碼通過asyncTask實現一個異步操作,並通過設置onRefreshListener監聽器調用onRefresh方法實現下拉時刷新,並在刷新完成後調用onRefreshComplete做復位處理。

今天就和大傢分享這些,有問題歡迎留言交流。

 摘自 notice501的專欄
 

發佈留言