SwipeRefreshLayout的基本使用「建议收藏」

SwipeRefreshLayout的基本使用「建议收藏」SwipeRefreshLayout的基本使用简介SwipRefreshLayout是谷歌前一段时间推出的一款下拉刷新控件。常用方法方法解释setColorSchemeResources(int…colorReslds)设置下拉进度条的颜色主题,参数可变,并且是资源id,最多设置四种不同的颜色。setProgressBackgroundSchemeResource(intcoloRes)设置下拉进度条的背景颜色,默认白色。isRefreshing()判断当前的

大家好,又见面了,我是你们的朋友全栈君。

SwipeRefreshLayout的基本使用

简介

SwipRefreshLayout是谷歌前一段时间推出的一款下拉刷新控件。

常用方法

方法 解释
setColorSchemeResources(int…colorReslds) 设置下拉进度条的颜色主题,参数可变,并且是资源id,最多设置四种不同的颜色。
setProgressBackgroundSchemeResource(int coloRes) 设置下拉进度条的背景颜色,默认白色。
isRefreshing() 判断当前的状态是否是刷新状态。
setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) 设置监听,需要重写onRefresh()方法,顶部下拉时会调用这个方法,在里面实现请求数据的逻辑,设置下拉进度条消失等等。
setRefreshing(boolean refreshing) 设置刷新状态,true表示正在刷新,false表示取消刷新。

使用

1.首先在应用或模块的 build.gradle 文件中添加所需工件的依赖项:

dependencies {
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
   
}

2.在xml文件里面添加相关代码

<?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="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout>
        
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

3.添加布局代码

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/swipeLayout">
    <ListView android:id="@+id/aa" android:layout_width="match_parent" android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>

在这里插入图片描述
4.setColorSchemeResources(int…colorReslds),可以改变下拉刷新时的颜色

public class MainActivity extends AppCompatActivity { 
   
    private SwipeRefreshLayout swipeRefreshLayout;
    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        SwipeRefreshLayout swip_refresh_layout=findViewById(R.id.swipeLayout);
        swip_refresh_layout.setColorSchemeResources(R.color.colorPrimary);
    }

在这里插入图片描述

5.setProgressBackgroundSchemeResource(int coloRes),设置下拉进度的背景颜色

public class MainActivity extends AppCompatActivity { 
   
    private SwipeRefreshLayout swipeRefreshLayout;
    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        SwipeRefreshLayout swip_refresh_layout=findViewById(R.id.swipeLayout);
        swip_refresh_layout.setColorSchemeResources(R.color.colorPrimary);      swip_refresh_layout.setProgressBackgroundColorSchemeColor(R.color.colorPrimaryDark);
    }

在这里插入图片描述

6.setRefreshing(boolean refreshing)设置刷新状态,false代表停止执行

swip_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
   
    @Override
    public void onRefresh() { 
   
        new Handler().postDelayed(new Runnable() { 
   
            @Override
            public void run() { 
   
                swip_refresh_layout.setRefreshing(false);
            }
        },2000);
    }
});

在这里插入图片描述
7.全部整理好后,再加上几个item,完整的代码如下

package com.example.swiperefreshlayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity { 
   
    private SwipeRefreshLayout swipeRefreshLayout;
    private String[] names = new String[]
            { 
   "Lion","Tiger","Monkey","Dog","Cat","Elephant"};
    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        //创建list集合
        ListView list = findViewById(R.id.aa);
        List<Map<String,Object>> listItems =
                new ArrayList<>();
        for (int i=0;i<names.length;i++)
        { 
   
            Map<String,Object> listItem =new HashMap<>();
            listItem.put("names",names[i]);
            listItems.add(listItem);
        }
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.item,
                new String[]{ 
   "names"}
                ,new int[]{ 
   R.id.names});
        list.setAdapter(simpleAdapter);
        //SwipeRefreshLayout功能介绍
        final SwipeRefreshLayout swip_refresh_layout=findViewById(R.id.swipeLayout);
        swip_refresh_layout.setColorSchemeResources(R.color.colorPrimary);
        swip_refresh_layout.setProgressBackgroundColorSchemeColor(R.color.colorPrimaryDark);
        swip_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
   
            @Override
            public void onRefresh() { 
   
                new Handler().postDelayed(new Runnable() { 
   
                    @Override
                    public void run() { 
   
                        swip_refresh_layout.setRefreshing(false);
                    }
                },2000);
            }
        });
    }


}

item.xml

<?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="match_parent">
    <TextView android:id="@+id/names" android:layout_width="match_parent" android:layout_height="70dp" android:paddingLeft="10dp" android:layout_marginTop="5dp" android:textColor="@color/colorPrimaryDark" android:textSize="30dp" />
</LinearLayout>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/swipeLayout">

        <ListView android:id="@+id/aa" android:layout_width="match_parent" android:layout_height="74dp" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

在这里插入图片描述

可能会遇到的错误

1.Error inflating class android.support.v4.widget.SwipeRefreshLayout

解决:android.support.v4.widget.SwipeRefreshLayout改为androidx.swiperefreshlayout.widget.SwipeRefreshLayout

参考

参考一
参考二

作者:胡恒娟
原文链接:https://blog.csdn.net/hhj98/article/details/106679237

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152918.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • NFS原理详解_简述NFS服务的工作流程

    NFS原理详解_简述NFS服务的工作流程【mike:前面大概看了看,后面看不懂】source:http://blog.51cto.com/atong/1343950NFS原理详解原创woshiliwentong2013-12-2312:17评论(4)24682人阅读PS:哈哈,这篇的篇幅真的非常的长。要看完真的要有很强的耐心那。我自己写也快写吐了呢。[ATon

    2025年7月17日
    1
  • Python—socket编程

    Python—socket编程

    2022年2月12日
    39
  • json_decode详解[通俗易懂]

    json_decode详解[通俗易懂]json_decode是php5.2.0之后新增的一个PHP内置函数,其作用是对JSON 格式的字符串进行编码.json_decode的语法规则:json_decode (&#1

    2022年7月1日
    27
  • 用flash做古诗动画_《古诗三首》Flash动画课件[通俗易懂]

    用flash做古诗动画_《古诗三首》Flash动画课件[通俗易懂]《古诗三首》Flash动画课件古诗词三首牧童[唐]吕岩草铺横野六七里,笛弄①晚风三四声。归来饱饭黄昏后,不脱蓑衣②卧月明。注释①弄:逗弄。②蓑衣:棕或草编的外衣,用来遮风挡雨。………舟过安仁①[宋]杨万里一叶渔船两小童,收篙②停棹③坐船中。怪生④无雨都张伞,不是遮头是使风。注释①安仁:县名。在湖南省东南部,宋时设县。②篙:撑船用的竹竿或木杆。③棹:船桨。④怪生:怪不得。……

    2022年5月5日
    83
  • 小波阈值去噪

    小波阈值去噪目录1.概念2.原理3.影响降噪效果的因素3.1小波基的选择3.2分解层数的选择3.3阈值的选择3.4阈值函数的选择4.MATLAB代码参考文献1.概念小波分析即用Mallat塔式算法对信号进行降阶分解。该算法在每尺度下将信号分解成近似分量与细节分量。近似分量表示信号的高尺度,即低频信息;细节分量表示信号的低尺度,即高频信息。对含有噪声的信号,噪声分量的主要能量集中在小波分解的细节分量中。2.原理小波阈值去噪的实质为抑制信号中无用部分、增强有用部分的过

    2022年6月16日
    52
  • mac 安装ll命令[通俗易懂]

    mac 安装ll命令[通俗易懂]mac安装ll命令mac-bash:ll:commandnotfound在linux系统下我们经常使用ll、la命令。但在mac系统时缺没有。提示:-bash:ll:commandnotfound。这是因为ll、la不是真的命令,而是一些常用命令和参数搭配的别名。所以我们在MacOS下配置下就好了。1、跳到个人目录下面localhost:~huangfy20$cdlocalhost:~huangfy20$pwd/Users/huangfy202、打开.bash

    2022年9月2日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号