gpt4 book ai didi

android - 嵌套的 RecyclerView 预取不适用于初始滚动

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:40 26 4
gpt4 key购买 nike

所以我的问题是我有一个垂直回收器 View ,其中包含多行嵌套的水平回收器 View 。我在嵌套的 recyclerviews 上启用了预取并将初始预取计数设置为 20(用于测试),但是,当在外部适配器的 onBindViewHolder 中添加项目时,由于内部 recyclerview 适配器上的未决更新,它不会在初始向下滚动时预取。这是我的外部适配器代码。每个“ block ”都是一个水平 ScrollView

private class BlockAdapter(val blocks: List<Block>) : RecyclerView.Adapter<VH>() {
private val viewCreatorsByType = HashMap<Int, Block>()

override fun getItemViewType(position: Int): Int {
val block = blocks[position]
val blockKey = block.viewType.ordinal
viewCreatorsByType[blockKey] = block
return blockKey
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val block = viewCreatorsByType[viewType]!!
return VH(block.createView(parent.context, parent))
}

override fun onBindViewHolder(holder: VH, position: Int) {
holder.index = position
holder.boundPresenter = blocks[position].presenter.apply {
attachView(holder.view)
resume()
present()
}
}

override fun onViewRecycled(holder: VH) {
holder.boundPresenter?.run {
pause()
detach()
}
holder.boundPresenter = null
}

override fun getItemCount(): Int = blocks.size
}

class VH(val view: android.view.View) : RecyclerView.ViewHolder(view) {
var boundPresenter: Presenter? = null
var index: Int = 0
}

当在 onBindViewHolder 中调用 present 时,填充内部适配器并将卡片添加到内部水平回收器 View 适配器,但由于水平回收器 View 适配器上的待更新而无法预取。这是在 GapWorker 类中找到的 ->

        void collectPrefetchPositionsFromView(RecyclerView view, boolean nested) {
mCount = 0;
if (mPrefetchArray != null) {
Arrays.fill(mPrefetchArray, -1);
}

final RecyclerView.LayoutManager layout = view.mLayout;
if (view.mAdapter != null
&& layout != null
&& layout.isItemPrefetchEnabled()) {
if (nested) {
// nested prefetch, only if no adapter updates pending. Note: we don't query
// view.hasPendingAdapterUpdates(), as first layout may not have occurred
if (!view.mAdapterHelper.hasPendingUpdates()) { //<-This is where prefetch is failing initially since the horizontal list adapter is has a pending "add" update.
layout.collectInitialPrefetchPositions(view.mAdapter.getItemCount(), this);
}
} else {
// momentum based prefetch, only if we trust current child/adapter state
if (!view.hasPendingAdapterUpdates()) {
layout.collectAdjacentPrefetchPositions(mPrefetchDx, mPrefetchDy,
view.mState, this);
}
}

if (mCount > layout.mPrefetchMaxCountObserved) {
layout.mPrefetchMaxCountObserved = mCount;
layout.mPrefetchMaxObservedInInitialPrefetch = nested;
view.mRecycler.updateViewCacheSize();
}
}
}

布局完成后,我可以向上滚动并按预期进行预取,但这对向下滚动时的初始卡顿没有帮助。这个问题有解决方案吗?

最佳答案

我认为你应该有一个 holder View ,然后创建你的水平 RecyclerView,然后使用 horizo​​ntalListHolder.addView(yourHorizo​​ntaRecyclerView) 将它们一个一个地添加到那个 holder,其中horizo​​ntalListHolder 可以是 ScrollView 中的垂直 LinearLayout所以你的布局看起来像这样:

<ScrollView
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:id="@+id/horizontalListHolder">
<--add your horizontal recyclerViews here !->
</LinearLayout>

</ScrollView>

希望对你有帮助

关于android - 嵌套的 RecyclerView 预取不适用于初始滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com