【发布时间】:2021-12-11 22:40:03
【问题描述】:
【问题讨论】:
-
还有many open source pull to refresh implementations,另外还有谷歌自己开源的
SwipeRefreshLayout。检查他们的实现,看看他们如何解决问题,如果现有解决方案都不符合您的需求,请使用它来得出您自己的解决方案。
标签: android kotlin material-design
【问题讨论】:
SwipeRefreshLayout。检查他们的实现,看看他们如何解决问题,如果现有解决方案都不符合您的需求,请使用它来得出您自己的解决方案。
标签: android kotlin material-design
首先添加:
implementation androidx.swiperefreshlayout:swiperefreshlayout:1.1.0
到您的 gradle 文件
XML 文件
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
//Add here your views
// I've got a RecyclerView so for what I need height is 0dp
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activity_main)
/* you probably have a few
code lines here*/
initRefreshListener()
}
.
.
.
private fun initRefreshListener() {
val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.refreshLayout)
swipeRefreshLayout.setOnRefreshListener {
Toast.makeText(this, "Refreshed", Toast.LENGTH_SHORT)
.show() // totally optional
// call API or what have you here
swipeRefreshLayout.isRefreshing = false
}
}
【讨论】: