`
dengyin2000
  • 浏览: 1225655 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android ListView pull up to refresh 改造.

阅读更多
在上一个一个博客中介绍了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();
    }
}

分享到:
评论
5 楼 pop1030123 2014-03-13  
cdhhappy 写道
博主您好,想请教您一个问题。
我之前也看过这个控件的实现,它有一个很大的问题就是当ListView的内容填不满一屏的时候,比如说ListView里面只有一项的时候,顶部的点击刷新的 button就不能隐藏了。
似乎新浪微薄的pullToRefresh ListView没有这个问题。

我现在在我的app里是自己实现了一个PullToRefresh控件,但是不是继承了ListView,而是从FrameLayout继承来的,所以当adapter的item数量较多的时候会对性能产生很大影响。非常容易crash。

对于这个问题的解决和新浪微薄客户端的是实现方法您有什么看法么。
多谢指教


请使用
https://github.com/fengcunhan/Android-PullToRefresh
4 楼 pop1030123 2014-03-13  
laineyhui 写道
我也遇到了同样的问题,就解答。谢谢

请使用
https://github.com/fengcunhan/Android-PullToRefresh
3 楼 guanting207 2013-05-31  
为什么我在模拟器上跑无法下拉啊
2 楼 laineyhui 2012-12-06  
我也遇到了同样的问题,就解答。谢谢
1 楼 cdhhappy 2011-10-09  
博主您好,想请教您一个问题。
我之前也看过这个控件的实现,它有一个很大的问题就是当ListView的内容填不满一屏的时候,比如说ListView里面只有一项的时候,顶部的点击刷新的 button就不能隐藏了。
似乎新浪微薄的pullToRefresh ListView没有这个问题。

我现在在我的app里是自己实现了一个PullToRefresh控件,但是不是继承了ListView,而是从FrameLayout继承来的,所以当adapter的item数量较多的时候会对性能产生很大影响。非常容易crash。

对于这个问题的解决和新浪微薄客户端的是实现方法您有什么看法么。
多谢指教

相关推荐

    Ultra-pull-to-refresh 三方库

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

    xlistview代码

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

    android listview拉动刷新

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

    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代码-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 实现滑动删除+下拉刷新加载更多功能整合

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

    android-pulltorefresh-listview

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

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

    首先,我们来看一下描述中提到的`pull_to_refresh.xml`布局文件,这是定义下拉刷新头部视图的关键部分。布局包含了一个`RelativeLayout`,里面有两个主要组件:一个`ImageView`和一个`ProgressBar`。`ImageView`通常...

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

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

    XMultiColumnListView

    // when disable pull refresh. protected RelativeLayout mHeaderViewContent; protected TextView mHeaderTimeView; protected int mHeaderViewHeight; // header view's height protected boolean ...

    Android高级应用源码-LearnPullToRefreshControls.zip

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

    下拉刷新,刷新显示时间

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

    ListView滑动效果

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

    【国开搜题】国家开放大学 一网一平台 Android核心开发技术02 期末考试押题试卷.docx

    adb pull:正确,用于从设备上拉取文件到本地。 **5. 自定义布局文件加载** - **知识点概述**: Android提供了LayoutInflater类来加载自定义布局文件。 - **选项分析**: - A. Load:非标准类名。 - B. Layout...

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

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

Global site tag (gtag.js) - Google Analytics