所以我的问题是我有一个垂直的回收站视图,其中包含一排嵌套的水平的回收站视图。我在嵌套的recyclerviews上启用了预取功能并将初始预取计数设置为20(用于测试),但是,由于在外部适配器的onBindViewHolder中添加项目时,由于内部recyclerview适配器上的未决更新,因此在初始向下滚动时未进行预取。这是我的外部适配器代码。每个“块”都是一个水平滚动视图
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中调用时,将填充内部适配器并将卡添加到内部水平回收站视图适配器,但是由于水平回收站视图适配器上的挂起更新而无法预取。在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();
}
}
}
一旦布局好,我就可以向上滚动并按预期进行预取,但这对向下滚动时的初始垃圾没有帮助。有解决这个问题的方法吗?
答案 0 :(得分:0)
我认为您应该拥有一个支架视图,然后创建水平RecyclerView
,然后使用horizontalListHolder.addView(yourHorizontaRecyclerView)
将它们horizontalListHolder
逐个添加到该支架,其中LinearLayout
可以是垂直{{ 1}}在ScrollView
中
因此您的布局应如下所示:
<ScrollView
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:id="@+id/horizontalListHolder">
<--add your horizontal recyclerViews here !->
</LinearLayout>
</ScrollView>
希望这会有所帮助