实际上,我是Paging Library的新手.这是一种情况,我正在从我的ViewModel观察PagedList,即使新闻列表出现在UI上也总是返回零.
viewModel.getNews().observe(this, news -> {
swipeRefreshLayout.setRefreshing(false);
Timber.d("news size is %s",news.size());// **news size is 0**
adapter.submitList(news);
});
Run Code Online (Sandbox Code Playgroud)
我的DataSource是
public class NewsDataSource extends ItemKeyedDataSource<String, News> {
private MutableLiveData<Resource.Status> netStatusLive = new MutableLiveData<>();
/*Hide Logic for clean purpose*/
private List<News> localNewsList = new ArrayList<>();
public NewsDataSource( /*Hide Logic for clean purpose*/) {
/*Hide Logic for clean purpose*/
}
@Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<News> callback) {
netStatusLive.postValue(Resource.Status.LOADING);
localNewsList.clear();
Disposable disposable = /*Hide Logic for …Run Code Online (Sandbox Code Playgroud) android android-architecture-components android-jetpack android-paging
在我的片段中,我集成了 android jetpack 分页库和数据源,我使用PageKeyedDataSource和改造 API 回调。
代码作为例外运行并将数据加载到recyclerview但在我滚动到底部后,它应该通过在数据源类中触发loadAfter函数来加载更多数据,但它没有
我也切换到ItemKeyedDataSource仍然无法执行
是我的代码错误还是插件有问题!但在我在 GitHub 中发现的一些演示应用程序中,我遵循了那里的代码。请如果有人有这个问题并修复让我知道编辑:使用AndroidX
public class ReportsDataSource extends PageKeyedDataSource<Integer, ReportItemModel> {
MutableLiveData<NetworkState> networkState = new MutableLiveData<>();
MutableLiveData<NetworkState> initialLoad = new MutableLiveData<>();
private UserService getUserService;
private List<Call<?>> compositeDisposable;
private CompositeDisposable compositeDisposableData;
private SchedulerProvider schedulerProvider;
private Completable retryCompletable = null;
public ReportsDataSource(UserService getUserService, List<Call<?>> compositeDisposable, CompositeDisposable compositeDisposableData, SchedulerProvider schedulerProvider) {
this.getUserService = getUserService;
this.compositeDisposable = compositeDisposable;
this.compositeDisposableData = compositeDisposableData;
this.schedulerProvider = schedulerProvider;
}
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我使用android分页库实现了分页。ViewModel 返回 aLiveData<PagedList<MyObject>>并且我观察到在我设置适配器的片段中一切正常,除了当我想检查列表的大小时始终为 0。
viewModel.getSearchResults().observe(this, mList -> {
adapter.submitList(mList);
Log.e("Count", String.valueOf(mList.size()));
});
Run Code Online (Sandbox Code Playgroud)
其中 mList 是 PagedList<MyObject>
android android-mvvm android-livedata android-paging android-paging-library
使用 android Paging 库,可以很容易地从数据库中分块加载数据,并且 ViewModel 提供自动 UI 更新和数据保存。所有这些框架模块帮助我们在 Android 平台上创建一个出色的应用程序。
典型的 Android 应用程序必须显示项目列表并允许用户搜索该列表。这就是我想通过我的应用程序实现的目标。所以我通过阅读很多文档、教程甚至stackoverflow的答案来完成了一个实现。但我不太确定我是否做得正确或者我应该如何做。下面,我展示了使用 ViewModel 和 RecyclerView 实现分页库的方法。
请检查我的实施并纠正我的错误或告诉我应该如何做。我认为有许多像我一样的新 Android 开发人员仍然对如何正确执行此操作感到困惑,因为没有单一来源可以回答您关于此类实现的所有问题。
我只展示我认为重要的东西。我正在使用房间。这是我正在使用的实体。
@Entity(tableName = "event")
public class Event {
@PrimaryKey(autoGenerate = true)
public int id;
public String title;
}
Run Code Online (Sandbox Code Playgroud)
这是事件实体的 DAO。
@Dao
public interface EventDao {
@Query("SELECT * FROM event WHERE event.title LIKE :searchTerm")
DataSource.Factory<Integer, Event> getFilteredEvent(String searchTerm);
}
Run Code Online (Sandbox Code Playgroud)
这是ViewModel扩展了AndroidViewModel ,它允许通过提供所有事件或根据搜索文本过滤的事件的LiveData< PagedList< Event>>来读取和搜索。我真的很挣扎,每次当filterEvent发生变化时,我都会创建新的LiveData,这可能是多余的或不好的。
private MutableLiveData<Event> filterEvent = new MutableLiveData<>();
private LiveData<PagedList<Event>> data;
private …Run Code Online (Sandbox Code Playgroud) android searchview android-recyclerview android-viewmodel android-paging
我是分页库 3.0 的新手,我只需要对本地数据库中的数据进行分页。最初,我从数据库中获取 PagingSource<Int, MessageDisplayModel> 并将其用作流。
fun getChatMessages(chatWrapper: ChatWrapper): Flow<PagingData<MessageDisplayModel>> {
return Pager(
config = PagingConfig(pageSize = 15,
maxSize = 50,
enablePlaceholders = true)
) {
xmppChatManager.getChatMessages(chatWrapper)
}.flow
}
Run Code Online (Sandbox Code Playgroud)
然后在 PagingData 通过 submitData() 方法传递给适配器之后。
lifecycleScope.launch {
@OptIn(ExperimentalCoroutinesApi::class)
mViewModel.getChatMessages(chatWrapper).collectLatest {
adapter.submitData(it) }
}
Run Code Online (Sandbox Code Playgroud)
现在,我关心的是如何从传递给适配器的 PagingData 中获取 MessageDisplayModel 的实际列表?
我试图检查第一个请求是否带有空对象,以显示一个布局,表明它没有项目。
我的解决方案是创建一个我自己的例外。我想知道是否有另一种更好的方法。因为我查看了文档并没有发现任何内容。
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Item> {
return try {
val position = params.key ?: FIRST_PAGE_INDEX
val response = api.getItem(position, params.loadSize, searchKey)
val nextKey = response?.next
val itemList = response?.itemList ?: emptyList()
if (itemList.isNotEmpty()) {
LoadResult.Page(
data = itemList,
prevKey = null,
nextKey = if (nextKey == LAST_PAGE_INDEX) null else nextKey
)
} else {
LoadResult.Error(EmptyListException())
}
} catch (exception: IOException) {
LoadResult.Error(exception)
} catch (exception: HttpException) {
LoadResult.Error(exception)
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用最新的 Jetpack 库。
Pagination3 版本:3.0.0-alpha05
房间版本:2.3.0-alpha02
我的实体有 Long asPrimaryKey和 Room 可以PagingSource为其他Int类型生成。
error: For now, Room only supports PagingSource with Key of type Int.
public abstract androidx.paging.PagingSource<java.lang.Long, com.example.myEntity>` getPagingSource();
Run Code Online (Sandbox Code Playgroud)
因此,我尝试实现我的 custom PagingSource,就像文档建议的那样。
问题是Data Refresh,因为 Room 生成的代码处理数据刷新,而我的代码无法处理这种情况。
任何建议如何实现自定义PagingSource的Room,也负责处理Data Refresh?
android android-room android-paging android-paging-library android-paging-3
我已经使用 Paging 实现了LazyColumn,但我现在也尝试添加粘性标题。
该stickyHeader()函数在作用域内不可用items(),所以我不知道它应该如何工作。
@Composable
fun MovieList(movies: Flow<PagingData<Movie>>) {
val lazyMovieItems: LazyPagingItems<Movie> = movies.collectAsLazyPagingItems()
LazyColumn {
// TODO: Add sticky headers
items(lazyMovieItems) { movie ->
MovieItem(movie = movie!!)
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何添加stickyHeaders?
android kotlin android-jetpack android-paging android-jetpack-compose
基本上我有一个在可组合函数中收集的分页数据流:
val list = state.listFlow.collectAsLazyPagingItems()
Run Code Online (Sandbox Code Playgroud)
列表中的每个项目都有一个号召性用语按钮,该按钮将启用/禁用视图并更新相应项目的 UI。
我的问题是,我们如何更新项目的视觉状态,而不需要在 PagingData 上调用刷新,从而重新查询 API/数据库以获取更新的数据?
此外,如果用户滚动浏览了 5 个或更多页面,我不想重新加载整个内容,更改可以是本地的。
有关如何实现这一目标的任何线索?
android android-paging android-jetpack-compose android-paging-3
我的要求是使用干净的架构和离线支持在页面中显示注释。我正在使用 Paging 库进行分页。下面是用于获取笔记的简洁架构图。
注意:请在新选项卡中打开上图并缩放以清晰查看。
我有四层UI、UseCase、Repository和Datasource。我打算抽象数据源的内部实现。为此,我需要NotesEntities在跨越边界之前映射到另一个模型。
class TimelineDao{
@Transaction
@Query("SELECT * FROM NotesEntities ORDER BY timeStamp DESC")
abstract fun getPagingSourceForNotes(): PagingSource<Int, NotesEntities>
}
Run Code Online (Sandbox Code Playgroud)
目前的实施:
internal class NotesLocalDataSourceImpl @Inject constructor(
private val notesDao: NotesDao
) : NotesLocalDataSource {
override suspend fun insertNotes(notes: NotesEntities) {
notesDao.insert(NotesEntities)
}
override fun getNotesPagingSource(): PagingSource<Int, NotesEntities> {
return notesDao.getPagingSourceForNotes()
}
}
Run Code Online (Sandbox Code Playgroud)
预期实施:
internal class NotesLocalDataSourceImpl @Inject constructor(
private val notesDao: NotesDao
) : NotesLocalDataSource {
override suspend …Run Code Online (Sandbox Code Playgroud) paging clean-architecture android-room android-paging android-paging-3
android-paging ×10
android ×9
android-room ×2
kotlin ×2
android-architecture-components ×1
android-mvvm ×1
paging ×1
searchview ×1