最新消息:文章中包含代码时,请遵守代码高亮规范!

打造属于自己的下拉刷新头部【原创】

Android Joy 4130浏览 1评论

首先介绍一款很牛逼的下拉刷新的框架android-Ultra-Pull-To-Refresh,里面有demo,大家可以去看一下效果。

我把链接放在这里:http://codekk.com/open-source-project-analysis/detail/Android/Grumoon/android-Ultra-Pull-To-Refresh%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90    下面把我自己做的demo给大家展示一下。

来看一下效果吧,只要换上自己喜欢的图片就OK了。

Screenshot_2015-11-24-21-40-28_com.grumoon.androidultrapulltorefreshdemo Screenshot_2015-11-24-21-40-24_com.grumoon.androidultrapulltorefreshdemo Screenshot_2015-11-24-21-40-20_com.grumoon.androidultrapulltorefreshdemo

 

 

 

 

 

 

 

 

 

首先我们要自己定义需要的头部布局

代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal"
    android:background="@color/grby">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="0.5">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iv_windmill"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:background="@mipmap/header"/>
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_head_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="30dp"
                android:gravity="bottom"
                android:text="下拉刷新"
                android:textColor="#ffffff"
                android:textSize="12sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_head_time"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:text="本次刷新时间 : 2015-10-23 12:00"
                android:textColor="#ffffff"
                android:textSize="10dp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

然后就可以去编写自己的实现文件了

public class WindmillHeader extends FrameLayout implements PtrUIHandler {

    private LayoutInflater inflater;

    // 下拉刷新视图(头部视图)
    private ViewGroup headView;

    // 下拉刷新文字
    private TextView tvHeadTitle;

    // 下拉图标
    private ImageView ivWindmill;

    // 下拉刷新时间
    private TextView tvHeadTime;

    /** 保存上一次的刷新时间. */
    private String lastRefreshTime = null;

    private WindmillDrawable drawable;

    public WindmillHeader(Context context) {
        this(context, null);
    }

    public WindmillHeader(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WindmillHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {

        inflater = LayoutInflater.from(context);
        /**
         * 头部
         */
        headView = (ViewGroup) inflater.inflate(R.layout.windmill_header, this, true);
        ivWindmill = (ImageView) headView.findViewById(R.id.iv_windmill);
        tvHeadTitle = (TextView) headView.findViewById(R.id.tv_head_title);
        tvHeadTime= (TextView) findViewById(R.id.tv_head_time);
        drawable = new WindmillDrawable(context, ivWindmill);
        ivWindmill.setImageDrawable(drawable);


    }

    @Override
    public void onUIReset(PtrFrameLayout ptrFrameLayout) {
        tvHeadTitle.setText("下拉刷新");
        drawable.stop();
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {
        tvHeadTitle.setText("下拉刷新");
        if(lastRefreshTime==null){
            lastRefreshTime = TimeUtils.getCurrentTimeInString();
            tvHeadTime.setText("当前刷新时间:" + lastRefreshTime);
        }else{
            tvHeadTime.setText("上次刷新时间:" + lastRefreshTime);
        }
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout ptrFrameLayout) {
        tvHeadTitle.setText("正在刷新");
        tvHeadTime.setText("本次刷新时间:" + lastRefreshTime);
        drawable.start();
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout ptrFrameLayout) {
        ivWindmill.clearAnimation();
        tvHeadTitle.setText("刷新完成");
        tvHeadTime.setText("本次刷新时间:" + lastRefreshTime);
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, int lastPos, int currentPos, float oldPercent, float currentPercent) {
        if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
            drawable.postRotation(currentPos - lastPos);
            invalidate();
        }

        final int mOffsetToRefresh = frame.getOffsetToRefresh();
        if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                tvHeadTitle.setText("下拉刷新");
                if(lastRefreshTime==null){
                    lastRefreshTime = TimeUtils.getCurrentTimeInString();
                    tvHeadTime.setText("当前刷新时间:" + lastRefreshTime);
                }else{
                    tvHeadTime.setText("上次刷新时间:" + lastRefreshTime);
                }
            }
        } else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                tvHeadTitle.setText("松开刷新");
                tvHeadTime.setText("上次刷新时间:" + lastRefreshTime);
                lastRefreshTime = TimeUtils.getCurrentTimeInString();
            }
        }


    }
}

有了上面的两个东西,我们就可以来实现刷新了

 

private WindmillHeader header;
private void initView() {

 ptr= (PtrFrameLayout) findViewById(R.id.ptr_main);

 header = new WindmillHeader(this);

 ptr.setHeaderView(header);
 ptr.addPtrUIHandler(header);
 ptr.disableWhenHorizontalMove(true);


 ptr.setPtrHandler(new PtrDefaultHandler() {

 @Override
 public void onRefreshBegin(final PtrFrameLayout frame) {
 new Handler().postDelayed(new Runnable() {
 @Override
 public void run() {
 // ptr.refreshComplete();
 getdata();
 }
 }, 3000);

 }

 @Override
 public boolean checkCanDoRefresh(PtrFrameLayout frame,
 View content, View header) {
 return PtrDefaultHandler.checkContentCanBePulledDown(frame,
 content, header);

 }
 });


 ptr.postDelayed(new Runnable() {
 @Override
 public void run() {
 ptr.autoRefresh(false);
 }
 }, 100);

}

这个demo是需要前面提到的那个框架的,大家可以去试一下。等你实现了,就会感觉很炫很棒的。

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/14639.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Joy的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!

网友最新评论 (1)

  1. 这个好像博客里面有人发过了
    Liphee2015-11-26 10:34