根据谷歌的 Yigit Boyar 的回应,Live Data 不是聊天应用程序的最佳用例,因为如果它们同时出现,它可能会丢失显示某些项目。他建议使用新的 Google 分页库。我一直在将 ItemKeyedDataSource 用于我的收件箱(所有向用户发送消息的人)和内部聊天(消息本身)。问题如下:
1- 从聊天中,当用户向下滚动时,用户检索旧消息,这意味着这些消息的插入应该在适配器的位置 0,而不是像分页库那样按顺序插入。如何将插入项目的位置依次交替用于新消息和位置 0 用于旧消息?
2-从收件箱(向用户发送消息的人),我再次在这里使用 ItemKeyedDataSource,问题是我想维护存储库中的多文档侦听器(我使用的是 Firebase Firestore),所以我可以检测每次有新人与用户交谈时。问题是callback.onResult它只被调用一次,并且在 Firebase 发送另一个用户时失败。如何维护可更新列表?
pagination android firebase google-cloud-firestore android-paging
我正在使用分页 3,除了初始加载状态外,一切正常。我正在添加withLoadStateFooter但它在第一次调用时从不显示加载状态
这是我的实现
负载状态适配器
class LoadStateAdapter (
private val retry: () -> Unit
): LoadStateAdapter<LoadStateViewHolder>() {
override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) {
holder.bindTo(loadState)
}
override fun onCreateViewHolder(
parent: ViewGroup,
loadState: LoadState
): LoadStateViewHolder {
return LoadStateViewHolder.create(parent, retry)
}
}
Run Code Online (Sandbox Code Playgroud)
加载状态视图持有者
class LoadStateViewHolder(
view : View,
private val retryCallback: () -> Unit
) : RecyclerView.ViewHolder(view) {
private val progressBar = view.findViewById<ProgressBar>(R.id.progress_bar)
private val errorMsg = view.findViewById<TextView>(R.id.error_msg)
private val btnRetry = view.findViewById<Button>(R.id.retry_button)
.also {
it.setOnClickListener { retryCallback() }
} …Run Code Online (Sandbox Code Playgroud) 我正在使用androidx.paging:paging-compose(v1.0.0-alpha-14)和Jetpack Compose(v1.0.3),我有一个PagingSource负责从后端提取项目的自定义。我还使用撰写导航组件。
问题是我不知道如何在通过 NavHostController 导航到不同屏幕和返回之间保存寻呼机流程的状态(滚动状态和缓存项目)。我试图通过保存状态,rememberSaveable但无法完成,因为它不是可以放入 Bundle 的东西。有一个快速/简单的步骤来做到这一点吗?
我的示例代码:
@Composable
fun SampleScreen(
composeNavController: NavHostController? = null,
myPagingSource: PagingSource<Int, MyItem>,
) {
val pager = remember { // rememberSaveable doesn't seems to work here
Pager(
config = PagingConfig(
pageSize = 25,
),
initialKey = 0,
pagingSourceFactory = myPagingSource
)
}
val lazyPagingItems = pager.flow.collectAsLazyPagingItems()
LazyColumn() {
itemsIndexed(items = lazyPagingItems) { index, item ->
MyRowItem(item) {
composeNavController?.navigate(...)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) android android-paging android-jetpack-compose android-paging-3
因此,在我的应用程序中,我正在使用 Compose Paging,但遇到了一个问题,我不确定如何正确处理。我来描述一下这个问题:
在我的分页列表中,我显示两种类型的项目:标题和实际数据单元格。我从 API 获取这些数据单元格的数据,并使用 insertSeparators 方法在它们之间插入标题。
我通过使用密封类来区分标题和单元格数据对象:
sealed class PagingModel {
data class Header(@StringRes val label: Int) : PagingModel()
data class CellData(val data: DataResponse) : PagingModel()
}
Run Code Online (Sandbox Code Playgroud)
因此,在分页源中,我将 api 结果映射到PagingModel.CellDatainsertSeparators 中,并在 insertSeparators 中添加PagingModel.Header一些之前/之后的逻辑。
现在,说到问题。我希望分页列表尽可能高效,因此我想在 LazyColumn 中使用 key 和 contentType 参数。这是一个代码片段:
items(
count = lazyPagingItems.itemCount,
key = lazyPagingItems.itemKey { **What do I put here when PagingData.Header has no key** },
contentType = lazyPagingItems.itemContentType {**should it just be "it" ?** }
) { index -> ...} …Run Code Online (Sandbox Code Playgroud) android android-paging android-jetpack-compose android-paging-3
我正在尝试将以下库添加到我的应用程序中
ext {
roomLibraryVersion = '1.1.0'
lifeCycleVersion = '1.1.1'
}
//room data base
implementation "android.arch.persistence.room:runtime:$roomLibraryVersion"
implementation "android.arch.persistence.room:compiler:$roomLibraryVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomLibraryVersion"
//life cycle
implementation "android.arch.lifecycle:runtime:$lifeCycleVersion"
implementation "android.arch.lifecycle:compiler:$lifeCycleVersion"
implementation "android.arch.lifecycle:extensions:$lifeCycleVersion"
// rx android
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation "io.reactivex.rxjava2:rxjava:2.1.14"
implementation 'android.arch.persistence.room:rxjava2:1.1.0'
//paging
implementation 'android.arch.paging:runtime:1.0.0'
Run Code Online (Sandbox Code Playgroud)
我在运行代码时遇到异常。任何人都可以帮我解决这个问题
Execution failed for task ':app:javaPreCompileDebug'.
> Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration.
- compiler-1.1.1.jar (android.arch.lifecycle:compiler:1.1.1)
Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = …Run Code Online (Sandbox Code Playgroud) 我在过去的两天里遇到了这个问题,我无法确定我在这里做错了什么。
情况是:我使用 Retrofit (POST 方法调用)将 2 个参数传递给我的 API,第一个参数是Token,第二个参数是CompanyId。我得到了大约 700 多个数据作为列表(JSON 响应)。所以我尝试实现 Android 架构的分页库来接收该列表中的 20 个项目。
然后,当加载 20 个项目时,再加载 20 个项目,以便我比平常更快地收到回复。
我的假设:我认为我的 API 的服务器页面没有分页。
我的问题是:是否可以在不分页 API 的情况下从 700 多个项目的列表中接收 20 个项目。
每次调用 API 时,加载 700 多个项目都需要花费太多时间。
在这种情况下如何使用分页库?
如果您需要我的代码的任何块,您可以在下面的评论中请求。
任何帮助将不胜感激,谢谢。
android android-recyclerview android-architecture-components android-paging
我试图学习如何将新的分页库与Kotlin语言一起使用已经有2天了(也是第一次)
因此,我已经阅读了许多指南/教程和Github存储库(https://github.com/STAR-ZERO/paging-retrofit-sample)来实现此分页库,基本上我的麻烦是在我的api调用之前,我的LiveData<PagedList<Discover>>内部ViewModel触发了即将结束,我不知道为什么,我觉得电话callback.onResult(it?.results.orEmpty(), null, 2)没有任何作用
我正在使用此版本android.arch.paging:runtime:1.0.1
您可以在这里找到我的项目的仓库:https : //github.com/florian-do/TMDB
logcat :
D/DataSourceFactory: : create()
D/SequentialDataSource: loadInitial:
D/Interceptor: https://api.themoviedb.org/3/discover/movie?api_key=??
D/MainFragment: : observe 0
D/SequentialDataSource: response code -> 200
D/SequentialDataSource: list size: 20
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
Fragment.kt
val adapter = DiscoverAdapter(context!!, diffCallBack)
binding.rvFeed.layoutManager = GridLayoutManager(context, 3)
binding.rvFeed.setHasFixedSize(true)
binding.rvFeed.adapter = adapter
viewModel.data.observe(this, Observer {
Log.d(TAG, ": observe "+it?.size)
})
Run Code Online (Sandbox Code Playgroud)
MainViewModel.kt
class MainViewModel : ViewModel() {
var amount = ObservableField<String>()
val data : LiveData<PagedList<Discover>>
init {
val …Run Code Online (Sandbox Code Playgroud) 我有一个带有搜索操作的操作栏菜单项。我有一个 recyclerview,它绑定来自加载 pagedlist 的 pagedlistadapter 的数据。我需要过滤适配器数据白色搜索视图文本更改,就像我们在其他适配器中所做的那样
我已经做了类似的事情,但它不起作用。
编辑:
这是我用于适配器的完整代码
public class ProductsRvAdapter extends PagedListAdapter<Product, ProductsRvAdapter.ViewHolder> implements Filterable {
private Context context;
public ProductsRvAdapter(Context context) {
super(DIFF_CALLBACK);
this.context = context;
}
private static DiffUtil.ItemCallback<Product> DIFF_CALLBACK = new DiffUtil.ItemCallback<Product>() {
@Override
public boolean areItemsTheSame(Product oldProduct, Product newProduct) {
return oldProduct.getId() == newProduct.getId();
}
@Override
public boolean areContentsTheSame(Product oldProduct, @NonNull Product newProduct) {
return oldProduct.equals(newProduct);
}
};
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item_product, viewGroup, false));
} …Run Code Online (Sandbox Code Playgroud) 我正在为 Jetpack 的 Paging 3 Library 苦苦挣扎。
我设置
在PagingSource由空间中创建的。
我知道RemoteMediators 的职责是从网络中获取项目并将它们保存到 Room 数据库中。通过这样做,我们可以使用 Room 数据库作为单一事实点。PagingSource只要我使用整数作为 .Room就可以轻松地为我创建nextPageKeys.
到现在为止还挺好。这是我的 ViewModel 来检索以下列表Sources:
private lateinit var _sources: Flow<PagingData<Source>>
val sources: Flow<PagingData<Source>>
get() = _sources
private fun fetchSources() = viewModelScope.launch {
_sources = sourcesRepository.getSources(
selectedRepositoryUuid,
selectedRef,
selectedPath
)
}
Run Code Online (Sandbox Code Playgroud)
val sources收集在Fragment.
fetchSources()每当三个参数之一发生变化时调用 ( selectedRepositoryUuid, selectedRefor selectedPath)
这是 Paging 调用的存储库 …
android android-room android-jetpack android-paging android-paging-library
我正在使用分页撰写库使用远程中介(由本地房间数据库支持)从服务器加载分页数据。有没有办法在滑动刷新的情况下手动刷新中介数据?
android android-paging android-jetpack-compose android-paging-3
android ×10
android-paging ×10
android-architecture-components ×1
android-room ×1
firebase ×1
kotlin ×1
pagination ×1
paging ×1