我刚刚查看了分页库,发现BoundaryCallback有三个方法。对于名为onItemAtFrontLoaded()的方法之一,文档说
Called when the item at the front of the PagedList has been loaded, and access has occurred within prefetchDistance of it.
Run Code Online (Sandbox Code Playgroud)
我不太确定什么时候会调用这个方法,有人可以给我一个详细的例子吗?
我已将页面大小设置为 10,观察者中的页面大小给出了正确的大小,但数据库(ROOM)中的所有项目都已加载到视图持有者中
边界回调onItemAtEndLoaded正在使用数据库中的最后一个项目进行调用。
这是我的配置:
public LiveData<PagedList<Design>> getDesignList(List<String> types, String idCode){
DataSource.Factory<Integer, Design> factory = mRepository.getDesigns(types, idCode);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(10)
.setPageSize(10)
.build();
return new LivePagedListBuilder<>(factory, pagedListConfig)
.setBoundaryCallback(**boundCallBack**)
.setFetchExecutor(mRepository.mIoExecutor)
.build();
}
Run Code Online (Sandbox Code Playgroud)
存储库(mRepository.getDesigns):
public DataSource.Factory<Integer, Design> getDesigns(List<String> types, String idCode) {
return designDao.getDesigns(types,idCode);
}
Run Code Online (Sandbox Code Playgroud)
Dao:(获取设计)
@Query("SELECT * FROM Design WHERE design_type IN (:types) AND id=:idCode ORDER BY design_id ASC")
DataSource.Factory<Integer, Design> getDesigns(List<String> types, String idCode);
Run Code Online (Sandbox Code Playgroud)
边界回调(boundCallBack):
public class BoundCall extends …Run Code Online (Sandbox Code Playgroud) android pagedlist android-room android-paging android-paging-library
使用paging 3.0,我成功地实现了它。现在我想为其添加搜索功能。
我只是显示照片库以及分页功能。现在我想在有人搜索时使分页无效
但每当我在搜索上调用无效时。应用程序崩溃..
照片片段.kt
@AndroidEntryPoint
class PhotosFragment : BaseFragment<FragmentPhotosBinding,PhotosFragmentViewModel>(R.layout.fragment_photos),
SearchView.OnQueryTextListener, LifecycleObserver {
override val mViewModel: PhotosFragmentViewModel by viewModels()
private lateinit var photoAdapter: PhotoCollectionAdapter
override fun onAttach(context: Context) {
super.onAttach(context)
activity?.lifecycle?.addObserver(this)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
///mViewModel.setFilter(getString(R.string.search_filter_default_value))
initAdapter()
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreated(){
mViewModel.trendingPhotos.observe(viewLifecycleOwner, Observer {
photoAdapter.submitData(lifecycle,it)
})
}
private fun initAdapter() {
photoAdapter = PhotoCollectionAdapter()
photoAdapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
mBinding.recyclerView.apply {
layoutManager = LinearLayoutManager(context)
setHasFixedSize(true)
adapter = photoAdapter
}
photoAdapter.addLoadStateListener { loadState -> …Run Code Online (Sandbox Code Playgroud) android kotlin android-viewmodel android-paging android-paging-3
从自定义分页实现迁移到Jetpack Paging 3 库后,数据未按预期加载。根据以下内容正确处理第一页:PagingConfigPager
internal fun createProductListPager(pagingSource: ProductListPagingSource): Pager<Int, Product> = Pager(
config = PagingConfig(
pageSize = 10,
prefetchDistance = 2,
),
initialKey = 0,
) { pagingSource }
Run Code Online (Sandbox Code Playgroud)
以下是摘录Adapter:
public class PagingProductCardAdapter(private val viewBinder: CoreViewBinder) :
PagingDataAdapter<Listable, RecyclerView.ViewHolder>(viewBinder.getDiffUtils()) {
public val list: List<Listable>
get() = snapshot().items
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
// ...
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
viewBinder.bind(list[position], holder)
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
当滚动到 …
我正在使用该Paging 3库,LazyColumn并且我想根据购物清单项目的类别对列表进行排序。在下面的代码中,LazyColumn抱怨它正在期待LazyPagingItems<TypeVariable(T)>该items属性,但发现了List<ShoppingListItem?>。我怎样才能解决这个问题?
可组合的
val lazyListState = rememberLazyListState()
val successItems = allItemsState.allItems?.collectAsLazyPagingItems()
LazyColumn(
state = lazyListState,
modifier = Modifier
.fillMaxWidth(),
contentPadding = PaddingValues(
start = 5.dp,
end = 5.dp,
top = 8.dp,
bottom = 165.dp
),
verticalArrangement = Arrangement.spacedBy(5.dp),
) {
val groupedByCategory = successItems!!.itemSnapshotList.groupBy { it!!.category }
groupedByCategory.forEach { (initial, shoppingListItems) ->
item {
Text(text = initial)
}
items(
items = shoppingListItems, //Throws error at this line
key …Run Code Online (Sandbox Code Playgroud) android android-paging android-jetpack-compose android-paging-3 android-jetpack-compose-lazy-column
我有一个 RecyclerView 与 Paging 一起实现,用于从房间数据库加载列表。当尺寸较小时,列表工作正常。当大小达到 50 - 60 左右时,列表仍然可以正常工作,但是当我切换到另一个片段然后返回到列表时,它会阻塞 UI 大约 1.5 - 2 秒,这在用户体验中非常沉闷(参见下面的 GIF) ):
我的代码如下:
DAO
@Query("SELECT * FROM account_table WHERE userID = :userID")
fun getAll(userID: String): DataSource.Factory<Int, Account>
Run Code Online (Sandbox Code Playgroud)
存储库
class AccountRepository private constructor(application: Application) {
private val database =
LockyDatabase.getDatabase(
application
)
private val accountDao = database.accountDao()
companion object {
@Volatile
private var instance: AccountRepository? = null
fun getInstance(application: Application) =
instance ?: synchronized(this) {
instance ?: AccountRepository(application).also { instance = it }
}
}
fun …Run Code Online (Sandbox Code Playgroud) android kotlin android-recyclerview android-paging android-paging-library
我正在使用 Android 分页库,如下所述:https : //developer.android.com/topic/libraries/architecture/paging.html
但我也有一个 EditText 用于按名称搜索国家/地区。
如何过滤 Paging 库中的结果以仅显示匹配的国家/地区?
public final LiveData> countriesPagedList;
public AllCountriesViewModel(@NonNull Application application) {
super(application);
appRepository = new AppRepository(application);
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(true)
.setPageSize(30)
.setInitialLoadSizeHint(10)
.setPrefetchDistance(50)
.build();
countriesPagedList = new LivePagedListBuilder(appRepository.getAllCountries(),config).build();
}
Run Code Online (Sandbox Code Playgroud) android ×6
kotlin ×2
android-jetpack-compose-lazy-column ×1
android-room ×1
pagedlist ×1
search ×1