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

开源项目之Android 向下刷新列表

Android PullToRefresh为 Android 应用提供一个向下滑动即刷新列表的功能。

  项目如图:

 

效果如图:

 


包含测试文件就2个目标文件!

主要类如下:


[java]
 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 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); 
    } 
 
    private void init(Context context) 
    { 
        //我们需要在代码中加载所有的动画,而不是通过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(); 
 
        mRefreshState = TAP_TO_REFRESH; 
 
        addHeaderView(mRefreshView); 
 
        super.setOnScrollListener(this); 
 
        measureView(mRefreshView); 
        mRefreshViewHeight = mRefreshView.getMeasuredHeight(); 
    } 
 
    @Override 
    protected void onAttachedToWindow() 
    { 
        super.onAttachedToWindow(); 
        setSelection(1); 
    } 
 
    @Override 
    public void setAdapter(ListAdapter adapter) 
    { 
        super.setAdapter(adapter); 
 
        setSelection(1); 
    } 
 
    /**
     * 设置将接收通知的侦听器,每次滚动列表。
     * @param l补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,