`

Android ListView pull up to refresh 改造(转)

 
阅读更多
转自: http://dengyin2000.iteye.com/blog/1187823
android的UI组件,其中就包括了一个android-pulltorefresh组件。

https://github.com/johannilsson/android-pulltorefresh

这个组件是用在twitter微博中的, 往下拉列表的话, 顶部会自动load, 而且有反弹的效果,非常cool。 但是它的自动load顶部的内部, 我现在需要拖拉listview到最后的时候load。这个组件不知道方向的选择, 所以自己就动手改了下,实现从底部load。 (今天发现新浪微博也实现了同样的功能。)  附件是源码。
com.markupartist.android.widget.PullToRefreshListView
package com.markupartist.android.widget;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
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.*;
import android.widget.AbsListView.OnScrollListener;
import com.markupartist.android.widget.pulltorefresh.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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 static final String TAG = "PullToRefreshListView";

    private OnRefreshListener mOnRefreshListener;

    /**
     * Listener that will receive notifications every time the list scrolls.
     */
    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 mRefreshOriginalBottomPadding;
    private int mLastMotionY;

    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);
    }

    private void init(Context context) {
        // Load all of the animations we need in code rather than through XML
        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);
        mRefreshView.setOnClickListener(new OnClickRefreshListener());
        mRefreshOriginalTopPadding = mRefreshView.getPaddingTop();
        mRefreshOriginalBottomPadding = mRefreshView.getPaddingBottom();

        mRefreshState = TAP_TO_REFRESH;

//        addHeaderView(mRefreshView);
        addFooterView(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);
    }

    /**
     * Set the listener that will receive notifications every time the list
     * scrolls.
     *
     * @param l The scroll listener.
     */
    @Override
    public void setOnScrollListener(OnScrollListener l) {
        mOnScrollListener = l;
    }

    /**
     * Register a callback to be invoked when this list should be refreshed.
     *
     * @param onRefreshListener The callback to run.
     */
    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        mOnRefreshListener = onRefreshListener;
    }

    /**
     * Set a text to represent when the list was last updated.
     *
     * @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();

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:

                if (!isVerticalScrollBarEnabled()) {
                    setVerticalScrollBarEnabled(true);
                }
                if (getLastVisiblePosition() == getAdapter().getCount() - 1
                        && mRefreshState != REFRESHING) {
                    if ((
                            mRefreshView.getTop() <= getMeasuredHeight() - mRefreshViewHeight)
                            && mRefreshState == RELEASE_TO_REFRESH) {
                        // Initiate the refresh
                        mRefreshState = REFRESHING;
                        prepareForRefresh();
                        onRefresh();
                    } else if (mRefreshView.getTop() > getMeasuredHeight() - mRefreshViewHeight) {
                        // Abort refresh and scroll down below the refresh view
                        resetHeader();
                        //setSelection(1);
                        if (getFooterViewsCount() > 0) {
                            setSelectionFromTop(getAdapter().getCount() - 1, (this.getMeasuredHeight()));
                        }

//                        if (mRefreshState != RELEASE_TO_REFRESH){
//                            scrollTo(0, mRefreshView.getScrollY() - mRefreshViewHeight);
//                        }
                    }
                }
                break;


            case MotionEvent.ACTION_DOWN:
                mLastMotionY = y;
                break;

            case MotionEvent.ACTION_MOVE:
                applyHeaderPadding(event);
                break;
        }

        return super.onTouchEvent(event);
    }

    private void applyHeaderPadding(MotionEvent ev) {
        final int historySize = ev.getHistorySize();

        // Workaround for getPointerCount() which is unavailable in 1.5
        // (it's always 1 in 1.5)
        int pointerCount = 1;
        try {
            Method method = MotionEvent.class.getMethod("getPointerCount");
            pointerCount = (Integer) method.invoke(ev);
        } catch (NoSuchMethodException e) {
            pointerCount = 1;
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (IllegalAccessException e) {
            System.err.println("unexpected " + e);
        } catch (InvocationTargetException e) {
            System.err.println("unexpected " + e);
        }

//        Log.i("PullToRefreshListView", "historySize:" + historySize);
//        Log.i("PullToRefreshListView", "pointerCount:" + pointerCount);
//        Log.i("PullToRefreshListView", "  ");
//        Log.i("PullToRefreshListView", "  ");
//        Log.i("PullToRefreshListView", "  ");

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

                    int historicalY = 0;
                    try {
                        // For Android > 2.0
                        Method method = MotionEvent.class.getMethod(
                                "getHistoricalY", Integer.TYPE, Integer.TYPE);
                        historicalY = ((Float) method.invoke(ev, p, h)).intValue();
                    } catch (NoSuchMethodException e) {
                        // For Android < 2.0
                        historicalY = (int) (ev.getHistoricalY(h));
                    } catch (IllegalArgumentException e) {
                        throw e;
                    } catch (IllegalAccessException e) {
                        System.err.println("unexpected " + e);
                    } catch (InvocationTargetException e) {
                        System.err.println("unexpected " + e);
                    }

                    // Calculate the padding to apply, we divide by 1.7 to
                    // simulate a more resistant effect during pull.
                    int topPadding = (int) (((historicalY + mLastMotionY)
                            + mRefreshViewHeight) / 1.7);

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

    /**
     * Sets the header padding back to original size.
     */
    private void resetHeaderPadding() {
        mRefreshView.setPadding(
                mRefreshView.getPaddingLeft(),
                mRefreshView.getPaddingTop(),
                mRefreshView.getPaddingRight(),
                mRefreshOriginalBottomPadding);
    }

    /**
     * Resets the header to the original state.
     */
    private void resetHeader() {
        if (mRefreshState != TAP_TO_REFRESH) {
            mRefreshState = TAP_TO_REFRESH;

            resetHeaderPadding();

            // Set refresh view text to the pull label
            mRefreshViewText.setText(R.string.pull_to_refresh_tap_label);
            // Replace refresh drawable with arrow drawable
            mRefreshViewImage.setImageResource(R.drawable.ic_pulltorefresh_arrow);
            // Clear the full rotation animation
            mRefreshViewImage.clearAnimation();
            // Hide progress bar and arrow.
            mRefreshViewImage.setVisibility(View.GONE);
            mRefreshViewProgress.setVisibility(View.GONE);
        }
    }

    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) {
//        Log.i("PullToRefreshListView", "firstVisibleItem:" + firstVisibleItem);
//        Log.i("PullToRefreshListView", "visibleItemCount:" + visibleItemCount);
//        Log.i("PullToRefreshListView", "totalItemCount:" + totalItemCount);
//        Log.i("PullToRefreshListView", "");
//        Log.i("PullToRefreshListView", "");
//        Log.i("PullToRefreshListView", "");
        // When the refresh view is completely visible, change the text to say
        // "Release to refresh..." and flip the arrow drawable.
        if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL
                && mRefreshState != REFRESHING) {
            if (firstVisibleItem + visibleItemCount == totalItemCount) {
                mRefreshViewImage.setVisibility(View.VISIBLE);
                if ((
                        mRefreshView.getTop() <= getMeasuredHeight() - mRefreshViewHeight)
                        && mRefreshState != RELEASE_TO_REFRESH) {
                    mRefreshViewText.setText(R.string.pull_to_refresh_release_label);
                    mRefreshViewImage.clearAnimation();
                    mRefreshViewImage.startAnimation(mFlipAnimation);
                    mRefreshState = RELEASE_TO_REFRESH;
                } else if (
                        mRefreshView.getTop() > getMeasuredHeight() - 20 - mRefreshViewHeight
                                && mRefreshState != PULL_TO_REFRESH) {
                    mRefreshViewText.setText(R.string.pull_to_refresh_pull_label);
                    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
                && getLastVisiblePosition() >= getAdapter().getCount() -1
                && mRefreshState != REFRESHING) {

                if (getFooterViewsCount() > 0) {
                    setSelectionFromTop(getAdapter().getCount() - 1, (this.getMeasuredHeight()));
                }


        }

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

    }

    public View getLoadBarView() {
        return mRefreshView;
    }

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

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

    public void prepareForRefresh() {
        resetHeaderPadding();

        mRefreshViewImage.setVisibility(View.GONE);
        // We need this hack, otherwise it will keep the previous drawable.
        mRefreshViewImage.setImageDrawable(null);
        mRefreshViewProgress.setVisibility(View.VISIBLE);

        // Set refresh view text to the refreshing label
        mRefreshViewText.setText(R.string.pull_to_refresh_refreshing_label);

        mRefreshState = REFRESHING;
    }

    public void onRefresh() {
        Log.d(TAG, "onRefresh");

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

    /**
     * Resets the list to a normal state after a refresh.
     *
     * @param lastUpdated Last updated at.
     */
    public void onRefreshComplete(CharSequence lastUpdated) {
        setLastUpdated(lastUpdated);
        onRefreshComplete();
    }

    /**
     * Resets the list to a normal state after a refresh.
     */
    public void onRefreshComplete() {
        Log.d(TAG, "onRefreshComplete");

        resetHeader();

        // If refresh view is visible when loading completes, scroll down to
        // the next item.
        if (mRefreshView.getBottom() > 0) {
            invalidateViews();
            //setSelection(1);
        }
    }

    /**
     * Invoked when the refresh view is clicked on. This is mainly used when
     * there's only a few items in the list and it's not possible to drag the
     * list.
     */
    private class OnClickRefreshListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            if (mRefreshState != REFRESHING) {
                prepareForRefresh();
                onRefresh();
            }
        }

    }

    /**
     * Interface definition for a callback to be invoked when list should be
     * refreshed.
     */
    public interface OnRefreshListener {
        /**
         * Called when the list should be refreshed.
         * <p/>
         * A call to {@link com.itaoo.view.PullToRefreshListView #onRefreshComplete()} is
         * expected to indicate that the refresh has completed.
         */
        public void onRefresh();
    }
}


分享到:
评论
1 楼 zhoujiamail 2011-12-02  
请问一下如果用这种方法实现头部刷新效果,当ListView里的列表项很少时是隐藏不掉的,有什么别的办法可以做到吗

相关推荐

    Android ListView pull up to refresh 改造.

    实现Pull-to-Refresh的方法有很多,其中一种是使用开源库,如`android-pulltorefresh`。这个库提供了易于集成的解决方案,允许开发者快速添加此功能。`pulltorefresh`目录中的代码可能包含了这个库的部分源码,供...

    Ultra-pull-to-refresh 三方库

    在Android应用开发中,下拉刷新(Pull-to-Refresh)功能已经成为许多应用的标准配置,它允许用户通过简单地向下拉动列表来更新内容。 Ultra-pull-to-refresh 是一个流行的开源第三方库,专为Android设计,提供了一种...

    android listview拉动刷新

    使用这个库,你可以自定义刷新头部视图,甚至实现上拉加载更多(Pull-up-to-load-more)的功能。 在`PullToRefreshActivity`这个示例文件中,可能包含了使用类似`android-pulltorefresh`库实现的拉动刷新ListView的...

    android listview 实现滑动删除+下拉刷新加载更多功能整合

    **下拉刷新(Pull-to-Refresh)** 下拉刷新功能允许用户通过在ListView顶部向下拉动来获取最新的数据。这在实时性要求较高的应用中尤其有用。实现下拉刷新一般包括以下步骤: 1. **集成下拉刷新库**:Android提供了...

    Android ListView 下拉上推

    一、下拉刷新(Pull to Refresh) 下拉刷新通常用于更新列表的最新数据。实现这一功能可以借助SwipeRefreshLayout库,它提供了一个可滑动刷新的容器,包括ListView、GridView等。以下步骤可以帮助你实现这个功能: ...

    Android上拉下拉自动刷新控件及其例子代码(Android Studio版本)

    This project aims to provide a reusable Pull to Refresh widget for Android. It was originally based on Johan Nilsson's library (mainly for graphics, strings and animations), but these have been ...

    android-pulltorefresh-listview

    2. 自定义刷新提示:使用`PullToRefreshBase.setMode()`方法选择下拉刷新模式,如仅上拉刷新(MODE_PULL_DOWN_TO_REFRESH)、仅下拉刷新(MODE_PULL_UP_TO_REFRESH)或两者都支持(MODE_BOTH)。 3. 提供动画效果:...

    xlistview代码

    * @description An ListView support (a) Pull down to refresh, (b) Pull up to load more. * Implement IXListViewListener, and see stopRefresh() / stopLoadMore(). */ package ...

    Android代码-Flutter下拉刷新和上拉加载组件。

    a widget provided to the flutter scroll component drop-down refresh and pull up load.support android and ios. If you are Chinese,click here(中文文档) (Suspend maintenance until 12 months after the end...

    Android自定义listview布局实现上拉加载下拉刷新功能

    然而,为了提供更好的用户体验,开发者经常需要在ListView中添加上拉加载(Load More)和下拉刷新(Pull to Refresh)的功能。本篇文章将详细讲解如何通过自定义ListView布局来实现这一功能。 首先,我们创建一个名...

    下拉刷新,刷新显示时间

    } else if (currentStatus == STATUS_PULL_TO_REFRESH) { // 松手时如果是下拉状态,就去调用隐藏下拉头的任务 new HideHeaderTask().execute(); } break; } // 时刻记得更新下拉头中的...

    Android自定义控件下拉刷新实例代码

    android:text="@string/pull_to_refresh" /&gt; android:id="@+id/updated_at" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_...

    ListView滑动效果

    4. **下拉刷新(Pull-to-Refresh)**:ListView的一个流行特性是下拉刷新,用户上滑列表到顶部时,可以触发刷新数据的操作,常用于更新网络数据。 5. **上拉加载(More)**:与下拉刷新类似,上拉加载允许用户在滑动到...

    Android侧滑删除+下拉刷新+上拉加载更多。

    其次,下拉刷新(Pull-to-Refresh)功能允许用户通过在列表顶部下拉来刷新数据。这种功能在新闻应用、社交媒体应用等中非常常见。Android中实现这一功能,可以使用SwipeRefreshLayout组件。设置SwipeRefreshLayout...

    Android项目仿新浪微博下拉刷新继承FrameLayout.rar

    在Android应用开发中,下拉刷新(Pull-to-Refresh)是一种常见的交互模式,用户通过下拉列表或视图来触发数据的更新。本项目“Android项目仿新浪微博下拉刷新继承FrameLayout”旨在实现一个类似新浪微博的下拉刷新...

    Android高级应用源码-LearnPullToRefreshControls.zip

    在众多Android应用开发中,下拉刷新(Pull-to-Refresh)功能是提升用户体验的重要手段之一。LearnPullToRefreshControls是一个专门研究和实现这一功能的开源项目,它为我们提供了深入理解下拉刷新机制及其在Android...

    安卓下拉上拉刷新相关-android-PullRefreshLayout.zip

    ### 下拉刷新(Pull-to-Refresh) 下拉刷新是一种用户界面设计模式,当用户在列表顶部向下滑动时,可以触发刷新操作。在Android中,Google提供了`SwipeRefreshLayout`组件来实现这一功能。`SwipeRefreshLayout`包含...

Global site tag (gtag.js) - Google Analytics