当前位置:操作系统 > 安卓/Android >>

android listivew 下拉回弹刷新

 

该效果是一名国外工程师(johannilsson)的代码,拿来研究了下,自己整合了一下,现在拿出来,跟大家一起分享。

 

再次感谢这位国外工程师(johannilsson),谢谢!

 

新浪微博,和QQ空间里面,都有那个下拉刷新的效果,另很多人眼前一亮,细细分析,原理原来如此。

 

在原作者的基础上,写了一些注释,和帮助大家更好的阅读理解,(可能其中有些地方注释不准,欢迎指正,谢谢)

 

下面,就亮出关键代码:

 

 

 

 ****  自定义listivew   (关键代码)

 

 

 

 

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;

 

    //箭头图片

    private static  int REFRESHICON = R.drawable.goicon;

  

    /**

     * listview 滚动易做图

     */

    private OnScrollListener mOnScrollListener;

   

    //视图索引器

    private LayoutInflater mInflater;

    /**

     * 头部视图  内容  -- start

     */

    private RelativeLayout mRefreshView;

    private TextView mRefreshViewText;

    private ImageView mRefreshViewImage;

    private ProgressBar mRefreshViewProgress;

    private TextView mRefreshViewLastUpdated;

    /**

     * 头部视图  内容  -- end

     */

    //当前listivew 的滚动状态

    private int mCurrentScrollState;

   

    //当前listview 的刷新状态

    private int mRefreshState;

 

    //动画效果

    //变为向下的箭头

    private RotateAnimation mFlipAnimation;

    //变为逆向的箭头

    private RotateAnimation mReverseFlipAnimation;

    //头视图的高度

    private int mRefreshViewHeight;

    //头视图 原始的top padding 属性值

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

    }

 

    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);//(R.layout.pull_to_refresh_header, null);

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

 

        mRefreshState = TAP_TO_REFRESH;

 

        addHeaderView(mRefreshView);

 

        super.setOnScrol

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,