效果图
关键代码
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
是一个容器视图,里面可以放listview
,recyclerview
等控件。当用户下拉控件时,刷新控件的内容。
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()
设置进度条的颜色主题,最多设置四种不同颜色