我有以下扩展功能 -
fun DataSource.Factory<Int, CountryEntity>.sortBy(comparator: Comparator<in CountryEntity>): DataSource.Factory<Int, CountryEntity> {
return mapByPage { list ->
list.sortedWith(comparator)
}
}
以及以下实现 -
class CountriesRepository(val viewmodel: ViewModel) {
.
.
.
fun getAllCountries(comparator: Comparator<in CountryEntity>?): LiveData<PagedList<CountryEntity>> {
if (comparator == null)
return countryDao.getAllCountries().toLiveData(10)
return countryDao.getAllCountries().sortBy(comparator).toLiveData(10)
}
}
//Fragment
override fun onOptionsItemSelected(item: MenuItem): Boolean {
var comparator: Comparator<CountryEntity>? = null
when (item.itemId) {
R.id.country_list_menu_order_by_country_name_ascending -> {
comparator = compareBy { country -> country.nativeName }
}
R.id.country_list_menu_order_by_country_name_descending -> {
comparator = compareByDescending { country -> country.nativeName }
}
R.id.country_list_menu_order_by_area_ascending -> {
comparator = compareBy { country -> country.area }
}
R.id.country_list_menu_order_by_area_descending -> {
comparator = compareByDescending { country -> country.area }
}
}
countriesViewModel.getAllCountries(comparator).observe(this, Observer { list ->
countriesAdapter.submitList(list)
})
return super.onOptionsItemSelected(item)
}
我面临的问题是列表确实得到了排序,但列表的可见部分没有,这意味着我需要向下滚动才能使列表自我攻击,而不是向上滚动以查看更新的数据。
此外,它似乎总是忽略列表中的第一项并且不显示它
我错过了什么?另一件事 - 我如何摆脱 ListAdapter 的动画?