Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 嵌套的RecyclerView预回迁仅在初始滚动上不起作用_Android_Android Recyclerview_Android Adapter - Fatal编程技术网

Android 嵌套的RecyclerView预回迁仅在初始滚动上不起作用

Android 嵌套的RecyclerView预回迁仅在初始滚动上不起作用,android,android-recyclerview,android-adapter,Android,Android Recyclerview,Android Adapter,所以我的问题是,我有一个垂直的回收器视图,其中有一行嵌套的水平回收器视图。我在嵌套的recyclerviews上启用了预回迁,并且初始预回迁计数设置为20(用于测试),但是,由于在外部适配器的onBindViewHolder中添加项目时内部recyclerview适配器上的更新挂起,在初始向下滚动时它不是预回迁。这是我的外部适配器代码。每个“块”都是一个水平滚动视图 private class BlockAdapter(val blocks: List<Block>) : Recyc

所以我的问题是,我有一个垂直的回收器视图,其中有一行嵌套的水平回收器视图。我在嵌套的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
}
私有类BlockAdapter(val blocks:List):RecyclerView.Adapter(){
private val viewCreatorsByType=HashMap()
覆盖getItemViewType(位置:Int):Int{
val块=块[位置]
val blockKey=block.viewType.ordinal
viewCreatorsByType[blockKey]=块
返回区块键
}
override fun onCreateViewHolder(父级:ViewGroup,viewType:Int):VH{
val block=viewCreatorsByType[viewType]!!
返回VH(block.createView(parent.context,parent))
}
覆盖BindViewHolder(支架:VH,位置:Int){
holder.index=位置
holder.boundPresenter=块[position].presenter.apply{
附件视图(支架视图)
简历()
现在()
}
}
覆盖视图(支架:VH){
holder.boundPresenter?运行{
暂停()
分离()
}
holder.boundPresenter=null
}
重写getItemCount():Int=blocks.size
}
VH类(val视图:android.view.view):RecyclerView.ViewHolder(视图){
var boundPresenter:Presenter?=null
var指数:Int=0
}
在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();
            }
        }
    }
void collectPrefetchPositionsFromView(RecyclerView视图,布尔嵌套){
mCount=0;
如果(mPrefetchArray!=null){
数组。填充(mPrefetchArray,-1);
}
最终RecyclerView.LayoutManager布局=view.mLayout;
如果(view.mAdapter!=null
&&布局!=null
&&layout.isItemPrefetchEnabled()){
如果(嵌套){
//嵌套预取,仅当没有挂起的适配器更新时。注意:我们不查询
//view.hasPendingAdapterUpdates(),因为可能没有出现第一个布局
如果(!view.mAdapterHelper.hasPendingUpdates()){//layout.mPrefetchMaxCountObserved){
layout.mPrefetchMaxCountObserved=mCount;
layout.mPrefetchMaxObservedInInitialPrefetch=嵌套;
view.mRecycler.updateViewCacheSize();
}
}
}

一旦布局完成,我可以向上滚动并按预期进行预取,但这对向下滚动时的初始jank没有帮助。有没有解决此问题的方法?

我认为您应该有一个保持架视图,然后创建水平
回收视图
,然后使用
horizontalListHold将它们逐个添加到该保持架中er.addView(YourHorizontaRecyclView)
中的
horizontalListHolder
可以是
ScrollView中的垂直
LinearLayout
因此,您的布局将如下所示:

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

</ScrollView>


希望这有帮助

我不知道我是否理解正确:因此,您有一个
RecyclerView
,您想添加一个项目,然后
onBindViewHolder
您想获取更多项目?或者您只想要可见的项目?我有一个垂直回收器视图,其中包含一行水平回收器视图,因此您可以向上滚动/down在垂直列表上,然后在每个水平列表上左/右。我还在“问题跟踪器”中创建了一个问题。它包含演示该问题的示例代码。@ReidMacif如果你解决了这个问题,请在我面临相同问题时更新答案。谢谢。@RaoArsalan我希望我能为你找到答案,但遗憾的是,我没有。我已经搬家了从该项目开始。感谢您的回复,但是,我需要使用回收器视图。滚动视图在这种情况下不起作用。出于性能原因,我们需要能够回收水平回收器视图。