下拉刷新控件SwipeRefreshLayout的使用介绍

SwipeRefreshLayout是官方提供的下拉刷新解决控件,具有使用简单,灵活等特点。

Android4.0以下的版本需要用到Android-support-v4.jar包才能用到 
Android-support-v4.jar包下载地址:https://download.csdn.net/download/qq_17798399/11261669

基本使用

在xml添加引入SwipeRefreshLayout的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

在Activity中使用SwipeRefreshLayout

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        //模拟网络请求需要3000毫秒,请求完成,设置setRefreshing 为false
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }, 3000);
    }
});

主要方法

(1)setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener

(2)setRefreshing(boolean): 显示或隐藏刷新进度条

(3)isRefreshing(): 检查是否处于刷新状态

(4)setColorSchemeResources(): 设置进度条的颜色主题,最多能设置四种

 

进阶

SwipeRefreshLayout 还提供了一些方法设置进度的样式,滑动的开始位置和结束位置等 

方法名作用
setSize(int size)设置进度View样式的大小,只有两个值DEFAULT和LARGE
setProgressViewOffset(boolean scale, int start, int end)设置进度View下拉的起始点和结束点,scale 是指设置是否需要放大或者缩小动画
setProgressViewEndTarget(boolean scale, int end)设置进度View下拉的结束点,scale 是指设置是否需要放大或者缩小动画
setDistanceToTriggerSync(int distance)设置触发刷新的距离
setOnChildScrollUpCallback(@Nullable OnChildScrollUpCallback callback)k如果child是自己自定义的view,可以通过这个回调,告诉swipeRefreshLayoutchild是否可以滑动

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值