[Android Material Design]组件07 - SwipeRefreshLayout

效果图

SwipeRefreshLayout

关键代码

xml布局文件代码如下:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/srl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="下拉刷新!" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

SwipeRefreshLayout是一个容器视图,里面可以放listviewrecyclerview等控件。当用户下拉控件时,刷新控件的内容。
kotlin逻辑代码如下:

val srl = findViewById<SwipeRefreshLayout>(R.id.srl)
val tv = findViewById<TextView>(R.id.tv)
srl.setColorSchemeColors(ContextCompat.getColor(this, android.R.color.holo_red_dark),
		ContextCompat.getColor(this, android.R.color.holo_orange_dark),
		ContextCompat.getColor(this, android.R.color.holo_blue_dark))
srl.setOnRefreshListener {
	srl.postDelayed({
		srl.isRefreshing = false
		tv.text = "刷新成功"
	}, 3000)
}

SwipeRefrshLayout控件可以实现下拉刷新的效果,基本使用的方法如下:

  • setOnRefreshListener(OnRefreshListener)
    添加下拉刷新监听器
  • setRefreshing(boolean)
    显示或者隐藏刷新进度条
  • isRefreshing()
    检查是否处于刷新状态
  • setColorSchemeResources()
    设置进度条的颜色主题,最多设置四种不同颜色

源码地址

https://github.com/yurensan/MaterialDesignDemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值