Rei*_*Mac 5 android android-adapter android-recyclerview
所以我的问题是我有一个垂直回收器视图,其中包含多行嵌套的水平回收器视图。我在嵌套的回收视图上启用了预取并将初始预取计数设置为 20(用于测试),但是,由于在外部适配器的 onBindViewHolder 中添加项目时内部回收视图适配器上的挂起更新,它不会在初始向下滚动时预取。这是我的外部适配器代码。每个“块”都是一个水平滚动视图
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
}
Run Code Online (Sandbox Code Playgroud)
在 onBindViewHolder 中调用 present 时,会填充内部适配器并将卡片添加到内部水平回收器视图适配器,但由于水平回收器视图适配器上的未决更新,它无法预取。这可以在 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
布局后,我可以向上滚动并按预期进行预取,但这对向下滚动时的初始卡顿没有帮助。这个问题有解决方案吗?
我认为你应该有一个持有者视图,然后创建水平方向RecyclerView,然后将它们一一添加到该持有者中horizontalListHolder.addView(yourHorizontaRecyclerView),其中可以是a 内的horizontalListHolder垂直方向
,因此你的布局看起来像这样:LinearLayoutScrollView
<ScrollView
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:id="@+id/horizontalListHolder">
<--add your horizontal recyclerViews here !->
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
986 次 |
| 最近记录: |