我目前的问题是,LoadStateAdapter显示加载和错误状态的 my 不在 my 内居中recyclerview,它有一个 gridlayout 作为布局管理器。我在官方 android 开发者网站上没有找到任何关于此的信息,所以我在这里问:如何将我的LoadStateAdapter内部集中在我的 Recyclerview 中?
@AndroidEntryPoint
class ShopFragment : Fragment(R.layout.fragment_shop), ShopAdapter.OnItemClickListener {
private val shopViewModel: ShopViewModel by viewModels()
private val shopBinding: FragmentShopBinding by viewBinding()
@Inject lateinit var shopListAdapter: ShopAdapter
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bindObjects()
}
private fun bindObjects() {
shopBinding.adapter = shopListAdapter.withLoadStateFooter(ShopLoadAdapter(shopListAdapter::retry))
shopListAdapter.clickHandler(this)
}
override fun onDestroyView() {
requireView().findViewById<RecyclerView>(R.id.rv_shop).adapter = null
super.onDestroyView()
}
}
Run Code Online (Sandbox Code Playgroud)
@FragmentScoped
class ShopLoadAdapter(private val retry: () …Run Code Online (Sandbox Code Playgroud) android kotlin android-recyclerview android-paging android-paging-3
在新的 Paging3 库的帮助下,它使我们可以轻松地在 recyclerview 中插入项目/分隔符,如 google android codelabs 教程https://developer.android.com/codelabs/android-paging#11所示,但是如何获取在 recyclerview 中每 n 个位置插入项目的逻辑是在每个位置 10 处插入项目。
示例代码
fun searchRepo(queryString: String): Flow<PagingData<UiModel>> {
val lastResult = currentSearchResult
if (queryString == currentQueryValue && lastResult != null) {
return lastResult
}
currentQueryValue = queryString
val newResult: Flow<PagingData<UiModel>> = repository.getSearchResultStream(queryString)
.map { pagingData -> pagingData.map { UiModel.RepoItem(it) } }
.map {
it.insertSeparators<UiModel.RepoItem, UiModel> { before, after ->
if (after == null) {
// we're at the end of the list
return@insertSeparators null
} …Run Code Online (Sandbox Code Playgroud) 我正在使用 Android Paging 3 beta 库在回收器视图中加载分页列表并使用 MVVM 架构。
我需要将其他元数据(例如有关页面的常见信息)传递给视图,但PagingSource.LoadResult.Page 的设计方式是仅将项目列表返回到视图。
以下是我想到的几个选项:
最好的方法是什么?还有其他更好的建议吗?
我正在尝试使用Android Paging Library 3实现基于游标的分页(基于GraphQL Relay 规范),但我不知道如何实现。getRefreshKey
这是我尝试过的:
data class PagingKey(
val before: String? = null,
val after: String? = null,
)
class MoviePagingSource() : PagingSource<PagingKey, Movie>() {
override suspend fun load(params: LoadParams<PagingKey>): LoadResult<PagingKey, Movie> {
return try {
val response = fetchMovies(
before = params.key?.before,
after = params.key?.after,
pageSize = params.loadSize,
)
LoadResult.Page(
data = response.edges.map { mapEdgeToMovie(it) },
prevKey = if (response.pageInfo.hasPreviousPage) PagingKey(
before = response.pageInfo.startCursor
) else null,
nextKey = if (response.pageInfo.hasNextPage) PagingKey(
after …Run Code Online (Sandbox Code Playgroud) android kotlin android-jetpack android-paging android-paging-3
嘿,我正在使用Paging 3库和ViewPager 2。它加载无限的数据。
\nimplementation "androidx.paging:paging-runtime-ktx:3.0.0-alpha07"\nRun Code Online (Sandbox Code Playgroud)\n数据源.kt
\npackage com.example.viewpagerexample\n\nimport java.util.*\n\nclass DataSource(\n private val size: Int = 5,\n private val currentDate: Date,\n private val limitDate: Date?\n) {\n\n fun returnData(pageNumber: Int): List<Date> {\n\n val dateList = mutableListOf<Date>()\n val startDateForPage = startDate(pageNumber)\n val tempCalendar = Calendar.getInstance()\n\n tempCalendar.time = startDateForPage\n val lastDateForPage = endDate(startDateForPage)\n\n while (tempCalendar.time < lastDateForPage) {\n if (limitDate == null ||\n tempCalendar.time.before(limitDate) ||\n tempCalendar.time == limitDate\n ) {\n dateList.add(tempCalendar.time)\n tempCalendar.add(Calendar.DATE, 1)\n } else {\n break\n …Run Code Online (Sandbox Code Playgroud) android kotlin android-paging android-paging-library android-paging-3
由于我目前正在开发一个带有自定义数据库(不是 Room)的项目,因此我正在测试是否可以在该项目中使用 Paging 3 库。但是,我遇到了一个问题,如果您对数据进行更改并因此使分页源无效,则列表的重新创建会出现错误并跳转到不同的位置。发生这种情况是因为刷新键计算似乎是错误的,这很可能是由于初始加载加载了三页数据,但将其放入一页而引起的。
默认的分页源如下所示:
override fun getRefreshKey(state: PagingState<Int, CustomData>): Int? {
// Try to find the page key of the closest page to anchorPosition, from
// either the prevKey or the nextKey, but you need to handle nullability
// here:
// * prevKey == null -> anchorPage is the first page.
// * nextKey == null -> anchorPage is the last page.
// * both prevKey and nextKey null -> anchorPage is the initial page, so
// just …Run Code Online (Sandbox Code Playgroud) android android-paging android-paging-library android-paging-3
我用于paging3在 中显示无尽的列表RecyclerView。当我单击特定项目时,我需要显示详细信息片段。详细信息片段包含一个视图分页器,其适配器也是PagingDataAdapter。也就是说,具有详细信息的片段应该显示我在上一屏幕上选择的元素,并且还可以水平滚动到也逐页加载的其他元素。问题是我无法从某个位置开始在视图寻呼机中显示分页列表。该列表始终从头开始显示。我找不到这个问题的解决方案。是否可以从特定位置开始监听页面源(RemoteMediator)的数据?请帮我:(
android android-view android-recyclerview android-viewpager2 android-paging-3
我有一个消息列表。每条消息都有一个唯一的 GUID。
我的设置适用于正常使用:用户单击对话,列表将打开,其中包含属于该对话的所有消息,按最新的顺序排列。
对话片段
@Override
public void onViewCreated(
@NonNull View view,
@Nullable Bundle savedInstanceState
) {
LifecycleOwner lifecycleOwner = getViewLifecycleOwner();
viewModel = new ViewModelProvider(this).get(ConversationViewModel.class);
viewModel
.getMessageList(lifecycleOwner, conversationId) // conversationId is a global variable
.observe(lifecycleOwner, messagePagingData -> adapter.submitData(
lifecycleOwner.getLifecycle(),
messagePagingData
));
super.onViewCreated(view, savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)
对话视图模型
final PagingConfig pagingConfig = new PagingConfig(10, 10, false, 20);
private final ConversationRepository conversationRepository;
public ConversationViewModel(@NonNull Application application) {
super(application);
conversationRepository = new ConversationRepository(application);
}
public LiveData<PagingData<ItemMessage>> getMessageList(
@NonNull LifecycleOwner lifecycleOwner,
@NonNull String conversationId
) {
return …Run Code Online (Sandbox Code Playgroud) 我当前的 Android 应用程序使用 Room Paging3 库
implementation 'androidx.paging:paging-runtime-ktx:3.0.0-beta01'
Run Code Online (Sandbox Code Playgroud)
我的要求是显示“数据加载微调器”、没有可用数据时的空消息或已加载的数据。
我可以成功显示加载微调器或加载时的数据,但是当没有数据可显示时,它不起作用。
我已经尝试过 SO 上提供的解决方案,但是没有一个能解决我的问题
我的代码成功处理加载和加载数据的版本类似于:-
viewLifecycleOwner.lifecycleScope.launch {
adapter.loadStateFlow
.distinctUntilChangedBy { it.refresh }
.filter { it.refresh is LoadState.NotLoading }
.flowOn(Dispatchers.Default)
.collectLatest {
withContext(Dispatchers.Main) {
if (adapter.itemCount == 0) (viewModel as HomeViewModel).loading.value = View.VISIBLE
else (viewModel as HomeViewModel).loading.value = View.GONE
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用这个投票给 SO 答案来显示空状态,但是它对我不起作用
adapter.addLoadStateListener { loadState ->
if (loadState.source.refresh is LoadState.NotLoading && loadState.append.endOfPaginationReached && adapter.itemCount < 1) {
recycleView?.isVisible = false
emptyView?.isVisible = true
} else {
recycleView?.isVisible = true …Run Code Online (Sandbox Code Playgroud) 我正在进行 2 个并行 API 调用来请求分页数据。我需要在获取结果后合并两个 API 调用的结果并将分页数据提交给适配器。
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
val assignedList = async {
assignedTaskViewModel.getPagingTasks(getAssignedTasks)
}
val unscheduledTasksList = async {
assignedTaskViewModel.getUnscheduledChores(getUnscheduledTasks)
}
unscheduledTasksList.await().combine(assignedList.await()) { unschedule, assign ->
Timber.d("combining 2 calls")
//TODO need to show unscheduledTasksList first followed by assignedList
}.collectLatest {
it.map { value ->
Timber.d("paging data = $value")
}
assignedTasksPagingAdapter?.submitData(it)
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法转换 pagingData,以便我可以合并 2 个 API 调用的结果并将其作为单个分页数据提交给适配器?