Android 下拉刷新控件的使用

Android 下拉刷新控件的使用


如上图,是下拉刷新控件,这种控件使用比较普遍,Google+、知乎等都有使用,下面介绍此控件的用法。

此控件是PullToRefreshLayout,在Github有源码: https://github.com/chrisbanes/ActionBar-PullToRefresh.git


    //在build.gradle中添加依赖库
    compile 'com.github.chrisbanes.actionbarpulltorefresh:library:+'

    //在xml中引用如下:
    <uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
        android:id="@+id/pull_refresh_layout" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent">

        <ListView
            android:layout_width="match_parent" 
            android:layout_height="match_parent" />

    </uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>

    //在Java代码中引用如下:
    pullRefreshLayout = (PullToRefreshLayout) findViewById(R.id.pull_refresh_layout);
    ActionBarPullToRefresh.from(this)
        .allChildrenArePullable()
        .listener(new OnRefreshListener() {
            @Override
            public void onRefreshStarted(View view) {
                //add your refreshing code.
            }
        })
        .setup(pullRefreshLayout);

通过以上代码,你就可以看到默认的效果了,如果需要自定义效果,请看如下:


    ....
    ActionBarPullToRefresh.from(this)
        .options(Options.create()
            .headerLayout(R.layout.pulltorefresh_header)  //重点是这儿,pulltorefresh_header里面自定义效果
            .build())
        .allChildrenArePullable()
    .....

pulltorefresh_header.xml代码如下:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">

    <FrameLayout
        android:id="@id/ptr_content" 
        android:layout_width="match_parent" 
        android:layout_height="48dp" 
        android:background="?android:attr/colorBackground">

        <TextView
            android:id="@id/ptr_text" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:gravity="center" />

    </FrameLayout>

    //上面的代码一般不作修改,主要的修改在下面,定义SmoothProgressBar的效果
    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:id="@id/ptr_progress" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:spb_stroke_separator_length="0dp" 
        app:spb_colors="@array/refresh_pb_colors" 
        android:minHeight="3dp"/>

</RelativeLayout>

自定义SmoothProgressBar的效果


<fr.castorflex.android.smoothprogressbar.SmoothProgressBar
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:spb_sections_count="4"  //颜色条的个数
    app:spb_color="#FF0000"   //进度条的颜色,单色
    app:spb_colors="@array/refresh_pb_colors"   //进度条的颜色,多色
    app:spb_speed="2.0"   //滚动速度,此处为2倍的速度
    app:spb_stroke_width="4dp"   //颜色条的宽度
    app:spb_stroke_separator_length="4dp"  //颜色条之间的间距
    app:spb_reversed="false"    //是否反向滚动
    app:spb_mirror_mode="false"    //是否为镜像显示模式
    app:spb_interpolator="spb_interpolator_accelerate"   //插值器
    app:spb_progressiveStart_activated="true"   //默认的滚动的
    app:spb_progressiveStart_speed="1.5"   //开始点的速度
    app:spb_progressiveStop_speed="3.4"    //结束点的速度
    />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值