HarmonyOS 鸿蒙Next下拉刷新 SwipeRefreshLayout
HarmonyOS 鸿蒙Next下拉刷新 SwipeRefreshLayout
下拉刷新 SwipeRefreshLayout
- 添加依赖
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
- 把想要实现下拉功能的控件放到SwipeRefreshLayout中
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipeRefreshLayout"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
- 添加刷新逻辑
swipeRefresh=binding.swipeRefreshLayout
swipeRefresh.setColorSchemeResources(R.color.colorPrimary) //给刷新的进度条设置颜色
swipeRefresh.setOnRefreshListener { //设置监听器,当用户下拉刷新时就会回调这里的lambda表达式
refreshFruit(adapter)
}
private fun refreshFruit(adapter: FruitAdapter){
thread{
Thread.sleep(2000)
runOnUiThread {
initFruit()
adapter.notifyDataSetChanged()
swipeRefresh.isRefreshing=false //表示刷新事件结束并隐藏刷新进度条
}
}
}
更多关于HarmonyOS 鸿蒙Next下拉刷新 SwipeRefreshLayout的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
在HarmonyOS中实现下拉刷新可以使用RefreshContainer
组件。这是鸿蒙Next推荐的下拉刷新容器,替代了Android的SwipeRefreshLayout。使用时需在ability_main.xml
中声明RefreshContainer
,并绑定RefreshController
控制刷新状态。通过onRefreshing
回调处理刷新逻辑,完成后调用finishRefresh()
结束刷新动画。该组件支持自定义刷新头部样式,需继承RefreshAnimator
类实现。
更多关于HarmonyOS 鸿蒙Next下拉刷新 SwipeRefreshLayout的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html