android - 如何在视图模型中使用 SwipeRefreshLayout?
问题描述
我正在 mvvm 中使用 SwipeRefreshLayout 进行开发。所以我在viewmodel和xml上写了代码。当我运行它时,进度条正在运行,但没有任何反应。日志不显示任何内容。请让我知道我应该解决什么问题。我使用了这个链接“ AndroidX SwipeRefreshLayout with DataBinding ”
xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.haii.schedulemanager.schedule.ScheduleViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:refreshing="@{viewmodel.isLoading}"
app:onRefreshListener="@{viewmodel::onRefresh}">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/scheduleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
</layout>
视图模型
class ScheduleViewModel @Inject constructor(
private val scheduleRepository: ScheduleRepository
) : ViewModel() {
private val _item = MutableLiveData<List<ScheduleItem>>()
val item: LiveData<List<ScheduleItem>> = _item
private val _oneSchedule = MutableLiveData<ScheduleItem>()
val oneSchedule: LiveData<ScheduleItem> = _oneSchedule
lateinit var mOneSchedule : ScheduleItem
private val _isLoading = MutableLiveData<Boolean>()
val isLoading: LiveData<Boolean> = _isLoading
fun getWeek(groupName : String){
//_isLoading.value = false
viewModelScope.launch {
val itemList = scheduleRepository.getWeek(groupName)
if(itemList.isEmpty()){
Log.d("TAG","Null")
_item.value=null
}else{
_item.value = itemList
}
//_isLoading.value = true
}
}
fun onRefresh(){
Log.d("TAG","onRefresh")
getWeek("Haii")
}
}
解决方案
刷新页面时不要忘记清除列表。
推荐阅读
- flutter - 颤动中下拉按钮的问题
- python - 为什么石灰表格方法会使用数据框生成类型错误?
- wpf - 如何更改默认 RadioRadioButton 的前景
- javascript - 隐藏/禁用客户端的 Firebase 功能
- php - PDO 定义()访问被拒绝
- c - 如何做一个矩阵指针数组?
- jquery - Jquery Slim Scroll 和 Angular JS,如何修复 div 底部的滚动?
- javascript - 文件上传 POST 请求时出现 JQuery 500 错误
- timezone - 如何使用 Quartz 调度程序在特定时区的特定时间执行一次作业
- javascript - 如何使用 Selenium Webdriver JavaScript 移动鼠标指针并单击?