当前位置: 首页 > news >正文

【Android】下拉刷新组件Swiperefreshlayout

效果如下所示:

需要在build.gradle里引入swiperefreshlayout的依赖

    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.swiperefreshlayout.widget.SwipeRefreshLayoutandroid:id="@+id/swipe_refresh_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="8dp"android:layout_marginLeft="8dp"android:layout_marginRight="8dp"android:layout_marginTop="8dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="parent"app:layout_constraintRight_toRightOf="parent"><TextViewandroid:id="@+id/tv_refresh"android:textSize="20sp"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="下拉开始刷新"/></androidx.swiperefreshlayout.widget.SwipeRefreshLayout></androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.swiperefreshlayout.widget.SwipeRefreshLayoutclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipe_refresh_layout)val tvRefreshText = findViewById<TextView>(R.id.tv_refresh)swipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.purple_200),ContextCompat.getColor(this, R.color.holo_green_light),ContextCompat.getColor(this, R.color.holo_orange_light),)swipeRefreshLayout.setOnRefreshListener {swipeRefreshLayout.postDelayed({swipeRefreshLayout.isRefreshing = falsetvRefreshText.text = "加载完成"}, 2000)}}
}

SwipeRefreshLayout 是 Android 提供的一个用于实现“下拉刷新”功能的控件,属于 androidx.swiperefreshlayout.widget.SwipeRefreshLayout 包。以下是它常用的 API 和说明。


✅ 常用 API 总结

1. setOnRefreshListener(OnRefreshListener listener)

  • 设置刷新监听器,当用户下拉时触发。

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Overridepublic void onRefresh() {// 执行刷新逻辑}
});

2. setRefreshing(boolean refreshing)

  • 设置当前是否处于刷新状态。你可以通过代码触发或停止刷新动画。

swipeRefreshLayout.setRefreshing(true);  // 显示刷新动画
swipeRefreshLayout.setRefreshing(false); // 停止刷新动画

3. isRefreshing()

  • 判断当前是否正在刷新。

if (swipeRefreshLayout.isRefreshing()) {// 说明正在刷新
}

4. setColorSchemeColors(int... colors)

  • 设置刷新动画的颜色(传入颜色值,不是资源 ID)。

swipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.colorPrimary),ContextCompat.getColor(this, R.color.colorAccent)
);

5. setProgressBackgroundColorSchemeColor(int color)

  • 设置刷新指示器的背景颜色。

swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(this, R.color.white)
);

6. setDistanceToTriggerSync(int distance)

  • 设置触发刷新所需下拉的距离(单位:像素)。

swipeRefreshLayout.setDistanceToTriggerSync(300);

7. setSize(int size)

  • 设置刷新指示器的大小,可选值有:

    • SwipeRefreshLayout.LARGE

    • SwipeRefreshLayout.DEFAULT

swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);

8. setProgressViewOffset(boolean scale, int start, int end)

  • 自定义刷新指示器的位置偏移。

swipeRefreshLayout.setProgressViewOffset(true, 0, 200);

📦 完整导包路径

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

🔍 官方文档链接

如果你想查看最新官方文档,可以访问:
https://developer.android.com/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout


相关文章:

  • 力扣算法---总结篇
  • 分式注记种表达方式arcgis
  • Qubes os系统详解
  • Leetcode 3542. Minimum Operations to Convert All Elements to Zero
  • Android之横向滑动列表
  • 每日算法刷题计划Day5 5.13:leetcode数组3道题,用时1h
  • mac 10.15.7 svn安装
  • 《内网渗透测试:绕过最新防火墙策略》
  • C#高级编程:设计模式原则
  • 数据分析预备篇---Pandas的Series
  • langChain存储文档片段,并进行相似性检索
  • 【神经网络与深度学习】通俗易懂的介绍非凸优化问题、梯度消失、梯度爆炸、模型的收敛、模型的发散
  • GITLAB跑gradle项目 不借助maven-publish直接上传到nexus私人仓库
  • C++STL——map和set的使用
  • 使用DevEco Studio性能分析工具高效解决鸿蒙原生应用内存问题
  • 免费专业级 PDF 处理!SolidPDF OCR 识别 + 精准转换批量处理
  • 【源码+文档+调试讲解】驾校报名小程序2
  • 嵌入式开发学习日志(数据结构--顺序结构单链表)Day19
  • 在scala中sparkSQL连接masql并添加新数据
  • 分割一切(SAM) 论文阅读:Segment Anything
  • 俄官员说将适时宣布与乌克兰谈判代表
  • 中国巴西关于乌克兰危机的联合声明
  • 图讯丨习近平出席中国-拉美和加勒比国家共同体论坛第四届部长级会议开幕式
  • 第二期人工智能能力建设研讨班在京开班,近40国和区域组织代表参加
  • 上海能源科技发展有限公司原董事长李海瑜一审获刑13年
  • 应急部:正在积极推动各地逐步科学建设改造应急避难场所