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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 简述mybatis框架与hibernate框架的区别_hibernate 性能

    简述mybatis框架与hibernate框架的区别_hibernate 性能hibernate与mybatis的区别和特点hibernate是全自动,而mybatis是半自动。hibernate完全可以通过对象关系模型实现对数据库的操作,拥有完整的JavaBean对象与数据库的映射结构来自动生成sql。而mybatis仅有基本的字段映射,对象数据以及对象实际关系仍然需要通过手写sql来实现和管理。hibernate数据库移植性远大于mybatis。hibernate通过它强大的映射结构和hql语言,大大降低了对象与数据库(oracle、mysql等)的耦合性,

    2022年9月4日
    3
  • Java性能优化的48条+七个案例

    Java性能优化的48条+七个案例Java性能优化的48条+七个案例

    2022年4月22日
    49
  • linux查看日志方法

    linux查看日志方法linux日志查看tail、head、cat、tac、sed、less、echo1、命令格式:tail[必要参数][选择参数][文件]-f循环读取-q不显示处理信息-v显示详细的处理信息-c<数目>显示的字节数-n<行数>显示行数-q,–quiet,–silent从不输出给出文件名的首部-s,–sleep-interval=S与-f合用,表示在每次反复的间隔休眠S秒tail-n…

    2022年6月23日
    30
  • 小程序父子组件传参_微信小程序修改全局变量

    小程序父子组件传参_微信小程序修改全局变量点击原创或者分类虽然样式如首页一样变化,但是其父组件的最终isActive的值并未发生改变,但是样式发生改变是因为拿取的是Component>里面的properties中的tabs,你点击下去的时候一样拿取tabs数组,所以不会报错。因此子组件必须通过方法进行修改父组件中的isActive的值,方法如下:components/Tabs/Tabs.js点击事件触发父组件中自定义事件同时传递数据给父组件this.triggerEvent(“父组件自定义事件的名称”,要传递的参数)…

    2022年9月5日
    3
  • python实现各大视频网站电影下载

    python实现各大视频网站电影下载一、前期准备有时候我们想下载自己喜欢的电影,但很多时候要么需要安装客户端才能下载,或者干脆不提供下载的服务,很是不爽,因此这里我们介绍使用python来实现网站的电影下载功能,凡是能在线观看的,都

    2022年7月3日
    24
  • linux日志审计系统_linux查看审计记录命令

    linux日志审计系统_linux查看审计记录命令Linux日志审计常用命令find、grep、egrep、awk、sedLinux中常见日志以及位置位置名称/var/log/cron记录了系统定时任务相关的日志/var/log/auth.log记录验证和授权方面的信息/var/log/secure同上,只是系统不同/var/log/btmp登录失败记录使用lastb命令查看/var/log/wtmp登录失成功记录使用last命令查看/var/log/lastlog最后一次登录

    2025年6月15日
    0

发表回复

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

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