大家好,我使用 Android Jetpack Paging 库 3,我正在创建一个实现网络 + 数据库场景的新闻应用程序,我正在关注 google 的 Codelab https://codelabs.developers.google.com/codelabs/android-paging,我几乎就像在 Codelab 中一样,我几乎匹配了示例中显示的所有操作https://github.com/android/architecture-components-samples/tree/main/PagingWithNetworkSample。
它几乎按其应有的方式工作......但我的后端响应是页面键控的,我的意思是响应带有新闻列表和下一页网址,远程中介获取数据,填充数据库,设置存储库,设置视图模型。 ..
问题是:当recyclerview加载数据时,会发生以下情况:recyclerview闪烁、项目跳转、被删除、再次添加等等。我不知道为什么 recyclerview 或其 itemanimator 的行为如此,看起来如此丑陋和故障。更重要的是,当我滚动到列表末尾时,会获取新项目,并且再次发生故障和跳跃效果。
如果您能帮助我,我将非常感激,我已经坐了三天了,提前非常感谢您。这是我的代码片段:
@Entity(tableName = "blogs")
data class Blog(
@PrimaryKey(autoGenerate = true)
val databaseid:Int,
@field:SerializedName("id")
val id: Int,
@field:SerializedName("title")
val title: String,
@field:SerializedName("image")
val image: String,
@field:SerializedName("date")
val date: String,
@field:SerializedName("share_link")
val shareLink: String,
@field:SerializedName("status")
val status: Int,
@field:SerializedName("url")
val url: String
) {
var categoryId: Int? = null
var tagId: Int? = null
}
Run Code Online (Sandbox Code Playgroud)
这是 DAO
@Insert(onConflict …Run Code Online (Sandbox Code Playgroud) android android-architecture-components android-paging android-paging-library android-paging-3
我试图为新的 Paging 3 库模仿 Google 的 codelab,当我尝试让 Room DAO 方法返回 a 时遇到以下错误PagingSource:
D:\Programming\Android\something\app\build\tmp\kapt3\stubs\debug\com\someapp\something\data\db\UsersDao.java:38: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser>).
public abstract androidx.paging.PagingSource<java.lang.Integer, com.someapp.something.data.db.GithubUser> getUserByUserName(@org.jetbrains.annotations.NotNull()
^D:\Programming\Android\something\app\build\tmp\kapt3\stubs\debug\com\someapp\something\data\db\UsersDao.java:43: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser>).
public abstract androidx.paging.PagingSource<java.lang.Integer, com.someapp.something.data.db.GithubUser> getUserByNote(@org.jetbrains.annotations.NotNull()
Run Code Online (Sandbox Code Playgroud)
这是我的UsersDao.kt:
@Dao
interface UsersDao {
@Insert
fun insert(user: GithubUser): Completable
@Insert
fun insert(userList: List<GithubUser>): Completable
@Query("DELETE FROM userDb")
fun clearDb(): Completable
@Query("SELECT * FROM …Run Code Online (Sandbox Code Playgroud) android rx-java2 android-room android-paging android-paging-library
Google 最近宣布了新的 Paging 3 库、Kotlin-first 库、对协程和 Flow 的支持等。
我玩过他们提供的代码实验室,但似乎还没有任何测试支持,我还检查了文档。他们没有提到任何关于测试的内容,所以例如我想对这个 PagingSource 进行单元测试:
class GithubPagingSource(private val service: GithubService,
private val query: String) : PagingSource<Int, Repo>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Repo> {
//params.key is null in loading first page in that case we would use constant GITHUB_STARTING_PAGE_INDEX
val position = params.key ?: GITHUB_STARTING_PAGE_INDEX
val apiQuery = query + IN_QUALIFIER
return try {
val response = service.searchRepos(apiQuery, position, params.loadSize)
val data = response.items
LoadResult.Page(
data,
if (position …Run Code Online (Sandbox Code Playgroud) We are trying to implement paging in Leanback VerticalGridSupportFragment with Architecture Components Paging Library. Leanback on it's own doesn't have any sort of out-of-box compatibility with Paging Library so we extended it's ObjectAdapter class and managed to implement append and clear operations quite easily but we are having a hard time trying to make modify operation work. During content modification operation, Paging Library's PagedList class computes the diff using AsyncPagedListDiffer which internally uses PagedStorageDiffHelper which is a package-private class and …
我将 Paging 3 与 RemoteMediator 结合使用,它在从网络获取新数据的同时显示缓存的数据。当我刷新我的PagingDataAdapter(通过调用refresh()它)时,我希望我的 RecyclerView 在刷新完成后滚动到顶部。在代码实验室loadStateFlow中,他们尝试通过以下方式处理这个问题:
lifecycleScope.launch {
adapter.loadStateFlow
// Only emit when REFRESH LoadState for RemoteMediator changes.
.distinctUntilChangedBy { it.refresh }
// Only react to cases where Remote REFRESH completes i.e., NotLoading.
.filter { it.refresh is LoadState.NotLoading }
.collect { binding.list.scrollToPosition(0) }
}
Run Code Online (Sandbox Code Playgroud)
这确实会向上滚动,但在 DiffUtil 完成之前。这意味着如果顶部确实插入了新数据,RecyclerView将不会一直向上滚动。
我知道 RecyclerView 适配器有一个AdapterDataObserver回调,当 DiffUtil 完成比较时我们可以收到通知。但这会导致适配器的各种竞争条件和PREPEND加载APPEND状态,这也会导致 DiffUtil 运行(但这里我们不想滚动到顶部)。
一种可行的解决方案是传递PagingData.empty()到PagingDataAdapter并重新运行相同的查询(仅调用refresh是行不通的,因为PagingData现在是空的并且没有任何内容可以刷新),但我更愿意保持旧数据可见,直到我知道刷新实际上成功了。
paging android android-paging android-paging-library android-paging-3
homeViewModel.pagingDataFlow.subscribe(expensesPagingData -> {
expenseAdapter.submitData(getLifecycle(), expensesPagingData);
}, throwable -> Log.e(TAG, "onCreate: " + throwable.getMessage()));
// ViewModel
private void init() {
pagingDataFlow = homeRepository.init();
CoroutineScope coroutineScope = ViewModelKt.getViewModelScope(this);
PagingRx.cachedIn(pagingDataFlow, coroutineScope);
}
// Repository
public Flowable<PagingData<ExpensesModel>> init() {
// Define Paging Source
expensePagingSource = new ExpensePagingSource(feDataService);
// Create new Pager
Pager<Integer, ExpensesModel> pager = new Pager<Integer, ExpensesModel>(
new PagingConfig(10,
10,
false,
10,
100),
() -> expensePagingSource); // set paging source
// inti Flowable
pagingDataFlow = PagingRx.getFlowable(pager);
return pagingDataFlow;
}
I have tried to …Run Code Online (Sandbox Code Playgroud) android android-recyclerview android-paging-library android-paging-3
我正在尝试按照Codelab使用房间数据库作为事实来源和 RemoteMediator 来实现 Jetpack paging 3 库。该应用程序查询 google books api,但由于某种原因,当我执行搜索时,它会多次调用同一页面。例如,当我在不滚动的情况下搜索 fire 时,我会在日志中看到以下内容:
D/BooksRepository: new search: fire
D/BooksRemoteMediator: title: fire, page: 0
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0 (648ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 1
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1 (608ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 0
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0 (629ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 1
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1 (843ms, …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
我在我的 android 项目中使用 androidx 分页库的 alpha 版本。它曾经工作得很好,但今天我的 Android 工作室开始显示有关PagedListAdapter类的弃用警告。我在google上搜索,也查看了android开发者网站上的官方文档,但没有找到任何东西。
我正在使用以下依赖项:
def paging_version = "3.0.0-alpha06"
implementation "androidx.paging:paging-runtime:$paging_version" //pagination
Run Code Online (Sandbox Code Playgroud)
这只是 Android 工作室的一个小故障还是已经被弃用了?
我将 Paging Library 3 与 RemoteMediator 一起使用,其中包括从网络和本地 Room 数据库加载数据。每次我滚动到 RecyclerView 中的某个位置,导航到另一个片段,然后导航回带有列表的片段时,滚动状态不会保留,并且 RecyclerView 显示从第一个项目开始的列表而不是位置在我离开之前我就已经到了。
我尝试使用 StateRestorationPolicy 但没有成功,并且似乎无法找到一种方法来获取 PagingDataAdapter 的滚动位置并在导航回 Fragment 时将其恢复到相同的精确位置。
在我的 ViewModel 中,我有一个从 RemoteMediator 收集数据的 Flow:
val flow = Pager(config = PagingConfig(5), remoteMediator = remoteMediator) {
dao?.getListAsPagingSource()!!
}.flow.cachedIn(viewModelScope)
Run Code Online (Sandbox Code Playgroud)
我正在将该数据提交给我的片段中的适配器:
viewLifecycleOwner.lifecycleScope.launch {
viewModel.flow.collectLatest { pagingData ->
adapter?.submitData(pagingData)
}
}
Run Code Online (Sandbox Code Playgroud)
在片段的顶部,我的适配器列出为:
class MyFragment : Fragment() {
...
private var adapter: FeedAdapter? = null
...
override onViewCreated(...) {
if (adapter == null) {
adapter = FeedAdapter(...)
}
recyclerView.adapter = adapter
viewLifecycleOwner.lifecycleScope.launch {
viewModel.flow.collectLatest …Run Code Online (Sandbox Code Playgroud) paging android android-paging android-paging-library android-paging-3
android ×10
android-room ×2
paging ×2
android-architecture-components ×1
android-tv ×1
androidx ×1
leanback ×1
rx-java2 ×1