Kotlin:nestedscrollview的问题,带回收库,从开始加载所有数据

发布于 2025-01-23 18:45:52 字数 9879 浏览 0 评论 0原文

我有一个片段,其中包含多个元素,一些文本视图,imageviews,一个用于广告的页面查看码器,在底部,我有一个recyclerview。我希望他们所有人都滚动为一个,所以我将其放置在嵌套的卷轴上。我将recyclerview高度设置为wrap_content并设置recyclerview.isnestedscrollingenabled = false。与小数据集一起使用一切正常,但是当我有一个大型数据集用于RecyClerview时,需要几秒钟并冻结UI。我看到一开始都在绘制整个数据集,并且它的行为不像是屏幕上绘制元素的回收道。告诉我这个。)我知道,在滚动小部件内有一个滚动的小部件并不是最好的做法,而是我拥有正确行为的唯一方法。是否有一种方法可以在嵌套的滚动视图中使用回收瓶,该视图仅加载回收瓶的一部分,但仍然滚动为一个?还是有更好的方法来构建这种布局?

(有可能有

binding.recyclerview.adapter = recyclerview_adapter()

在其他/背景线程上?我不这么认为,因为它执行UI操作)

(我尝试为回收器视图设置特定的高度,但它独立于嵌套的scrollview滚动)

xml

 < androidx.swiperefreshlayout.widget.swiperefreshlayout
    android:id =“@+id/inicio_swipe_container”
    android:layout_width =“ match_parent”
    android:layout_height =“ match_parent”
    应用:layout_constraintbottom_tobottomof =“ parent”
    应用:layout_constraintend_toendof =“ parent”
    应用:layout_constraintstart_tostartof =“ parent”
    应用:layout_constrainttop_totopof =“ parent”>

    < androidx.core.widget.nestedscrollview
        Android:ID =“@+ID/NestedScrollview”
        android:layout_width =“ match_parent”
        android:layout_height =“ match_parent”
        应用:layout_constraintbottom_tobottomof =“ parent”
        应用:layout_constraintend_toendof =“ parent”
        应用:layout_constraintstart_tostartof =“ parent”
        应用:layout_constrainttop_totopof =“ parent”>

        < androidx.constraintlayout.widget.constraintlayout
            android:layout_width =“ match_parent”
            android:layout_height =“ match_parent”>

            < androidx.constraintlayout.widget.constraintlayout
                android:id =“@+id/”
                android:layout_width =“ match_parent”
                android:layout_height =“ wrap_content”
                Android:layout_margintop =“ 8dp”
                Android:minheight =“ 300DP”
                应用:layout_constraintbottom_totopof =“@+id/”
                应用:layout_constraintend_toendof =“ parent”
                应用:layout_constrainthorizo​​ntal_bias =“ 0.5”
                应用:layout_constraintstart_tostartof =“ parent”
                app:layout_constrainttop_tobottomof =“@+id/”>

                < textView
                    android:id =“@+id/”
                    android:layout_width =“ 0dp”
                    android:layout_height =“ wrap_content”
                    android:layout_marginstart =“ 8dp”
                    Android:layout_marginend =“ 8dp”
                    Android:text =“”
                    Android:textColor =“@color/colorprimarydark”
                    Android:TextStyle =“ Bold”
                    应用:layout_constraintend_toendof =“ parent”
                    应用:layout_constraintstart_tostartof =“ parent”
                    应用:layout_constrainttop_totopof =“ parent” />

                < androidx.ViewPager.Widget.ViewPager
                    android:id =“@+id/”
                    android:layout_width =“ 0dp”
                    android:layout_height =“ 0dp”
                    Android:layout_marginstart =“ 16dp”
                    Android:layout_margintop =“ 8dp”
                    Android:layout_marginend =“ 16dp”
                    android:layout_marginbottom =“ 4DP”
                    Android:minheight =“ 200DP”
                    应用:layout_constraintbottom_totopof =“@+id/”
                    应用:layout_constraintend_toendof =“ parent”
                    应用:layout_constraintstart_tostartof =“ parent”
                    app:layout_constrainttop_tobottomof =“@+id /” />

                < linearlayout
                    android:id =“@+id/”
                    android:layout_width =“ 0dp”
                    android:layout_height =“ wrap_content”
                    Android:layout_marginstart =“ 16dp”
                    android:layout_marginend =“ 16dp”
                    Android:layout_marginbottom =“ 8dp”
                    Android:重力=“ Center_vertical | Center_horizo​​ntal”
                    android:entirentation =“水平”
                    应用:layout_constraintbottom_tobottomof =“ parent”
                    应用:layout_constraintend_toendof =“ parent”
                    应用:layout_constrainthorizo​​ntal_bias =“ 0.0”
                    应用:layout_constraintstart_tostartof =“ parent”></linearlayout>

                < progressbar
                    android:id =“@+id/”
                    style =“?android:atter/progressbarstyle”
                    android:layout_width =“ wrap_content”
                    android:layout_height =“ wrap_content”
                    应用:layout_constraintbottom_tobottomof =“ parent”
                    应用:layout_constraintend_toendof =“ parent”
                    应用:layout_constraintstart_tostartof =“ parent”
                    应用:layout_constrainttop_totopof =“ parent” />

            </androidx.constraintlayout.widget.constraintlayout>

            < androidx.constraintlayout.widget.constraintlayout
                android:id =“@+id/”
                android:layout_width =“ match_parent”
                android:layout_height =“ wrap_content”
                Android:minheight =“ 200DP”
                应用:layout_constraintbottom_tobottomof =“ parent”
                应用:layout_constraintend_toendof =“ parent”
                应用:layout_constrainthorizo​​ntal_bias =“ 0.5”
                应用:layout_constraintstart_tostartof =“ parent”
                app:layout_constrainttop_tobottomof =“@+id/dendaintlayout_inicio_avisos”>

                < textView
                    android:id =“@+id/”
                    android:layout_width =“ 0dp”
                    android:layout_height =“ 1dp”
                    android:background =“#c6c2c2”
                    Android:text =“ textView”
                    应用:layout_constraintend_toendof =“ parent”
                    应用:layout_constraintstart_tostartof =“ parent”
                    应用:layout_constrainttop_totopof =“ parent” />


                < imageView
                    android:id =“@+id/”
                    android:layout_width =“ 0dp”
                    android:layout_height =“ 75dp”
                    Android:layout_marginstart =“ 24dp”
                    android:layout_margintop =“ 4DP”
                    android:layout_marginend =“ 24dp”
                    android:src =“@dr​​awable/centralimperdibles”
                    应用:layout_constraintend_toendof =“ parent”
                    应用:layout_constraintstart_tostartof =“ parent”
                    app:layout_constrainttop_tobottomof =“@+id /line1” />

                < androidx.RecyClerview.Widget.RecyClerview
                    android:id =“@+id/recyclerview”
                    android:layout_width =“ 0dp”
                    android:layout_height =“ wrap_content”
                    Android:layout_marginbottom =“ 24dp”
                    应用:layout_constraintbottom_tobottomof =“ parent”
                    应用:layout_constraintend_toendof =“ parent”
                    应用:layout_constraintstart_tostartof =“ parent”
                    app:layout_constrainttop_tobottomof =“@+id /” />

                < progressbar
                    android:id =“@+id/”
                    style =“?android:atter/progressbarstyle”
                    android:layout_width =“ wrap_content”
                    android:layout_height =“ wrap_content”
                    应用:layout_constraintbottom_tobottomof =“ parent”
                    应用:layout_constraintend_toendof =“ parent”
                    应用:layout_constraintstart_tostartof =“ parent”
                    应用:layout_constrainttop_totopof =“ parent” />

            </androidx.constraintlayout.widget.constraintlayout>



        </androidx.constraintlayout.widget.constraintlayout>
    </androidx.core.widget.nestedscrollview>

</androidx.swiperefreshlayout.widget.swiperefreshlayout>
 

分段

val grid = gridlayoutmanager(活动,2,recyclerview.tervical,false) binding.recyclerview.layoutmanager =网格 binding.RecyClerview.SethasAsfixedSize(true) binding.recyclerview.isnestedscrollingenabled = false binding.recyclerview.adapter = recyclerview_adapter()

理想的行为是,当您向下滚动时,顶部小部件会消失,直到回收器视图为整个屏幕高度,然后recyclerview可以滚动。

谢谢。

编辑

试图在回收器视图中使用,但更新不起作用。如果我重置适配器(评论的部分),则可以慢慢,因为它绘制了40个项目。为什么NotifyIteMrangeSerted不起作用?

binding.NestedScrollView.setOnScrollChangeListener(androidx.core.widget.NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->

        if (scrollY >= binding.ConstraintLayoutInicio.measuredHeight - v.measuredHeight - 1000) {
            if(bottomOfScroll == 0) {
                println("BOTTOM SCROLL")

                if (ProductosForListaParts.value!![0].Id != "0") {
                    val newcount = ProductosForListaParts.value!!.count() + 20
                    var productosListPartsToAdd: ArrayList<Producto> = ArrayList()
                    ProductosForLista.value?.forEachIndexed { i, producto ->
                        if (i >= newcount-20 && i < newcount) {
                            productosListPartsToAdd.add(producto)
                        }
                    }
                    ProductosForListaParts.value!!.addAll(productosListPartsToAdd)
                    
                    binding.RecyclerViewHotBuy.adapter?.notifyItemRangeInserted(newcount - 20, 20)
                    
                    //binding.RecyclerViewHotBuy.adapter = Inicio_Producto_Adapter(
                    //    ProductosForListaParts.value!!, OrganizationDollar.value!!, SubirAlCarritoProductos,
                    //    CarritoProductosArray, BajarDelCarritoProductos, event, eventPesables)
                }

            }
        }
    })

I have a fragment that has multiple elements in it, some textViews, imageViews, a page ViewPager for ads and at the bottom I have a recyclerView. I want them all to scroll as one so I places then in a nested scrollView. I set the recyclerView height to wrap_content and set the recyclerView.isNestedScrollingEnabled = false. Everything works fine with small dataset but when I have a large dataset for the recyclerView it takes a few second and freeze the UI. I see that the whole dataset is being is being drawn at the start and its not behaving like a recyclerView that only draws elements on the screen.(I know why this happen, because the recyclerView is as tall as the dataset, so please don't tell me this.). I know that having a scrolling widget inside a scrolling widget is not best practice but its the only way I have got the correct behavior to work. Is there a way to have a recyclerView in a nested scroll view that only loads the part of the recyclerView but still scrolls as one? Or is there a better way to structure this layout?

(Is it posible to have the

binding.RecyclerView.adapter = RecyclerView_Adapter()

on a different/background thread? I don't think so because its doing UI operations)

(I have tried setting a specific height to the recycler view but it scrolls independently from the Nested scrollView )

XML

 <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/inicio_swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/NestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:minHeight="300dp"
                app:layout_constraintBottom_toTopOf="@+id/"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/">

                <TextView
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    android:text=""
                    android:textColor="@color/colorPrimaryDark"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="4dp"
                    android:minHeight="200dp"
                    app:layout_constraintBottom_toTopOf="@+id/"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/" />

                <LinearLayout
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="8dp"
                    android:gravity="center_vertical|center_horizontal"
                    android:orientation="horizontal"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"></LinearLayout>

                <ProgressBar
                    android:id="@+id/"
                    style="?android:attr/progressBarStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="200dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/ConstraintLayout_Inicio_Avisos">

                <TextView
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:background="#c6c2c2"
                    android:text="TextView"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />


                <ImageView
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="75dp"
                    android:layout_marginStart="24dp"
                    android:layout_marginTop="4dp"
                    android:layout_marginEnd="24dp"
                    android:src="@drawable/centralimperdibles"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/line1" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="24dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/" />

                <ProgressBar
                    android:id="@+id/"
                    style="?android:attr/progressBarStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>



        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

fragment

val Grid = GridLayoutManager(activity, 2, RecyclerView.VERTICAL,false)
binding.RecyclerView.layoutManager = Grid
binding.RecyclerView.setHasFixedSize(true)
binding.RecyclerView.isNestedScrollingEnabled = false
binding.RecyclerView.adapter = RecyclerView_Adapter()

The ideal behavior is that as you scroll down the top widgets disappear until the recycler view is the whole screen height and then the recyclerView can scroll.

Thank you.

EDIT

Trying to pagina the recycler view but the update not working. If I reset the adapter(the section commented out) it works but its slow because it drawing 40 items. Why is notifyItemRangeInserted not working?

binding.NestedScrollView.setOnScrollChangeListener(androidx.core.widget.NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->

        if (scrollY >= binding.ConstraintLayoutInicio.measuredHeight - v.measuredHeight - 1000) {
            if(bottomOfScroll == 0) {
                println("BOTTOM SCROLL")

                if (ProductosForListaParts.value!![0].Id != "0") {
                    val newcount = ProductosForListaParts.value!!.count() + 20
                    var productosListPartsToAdd: ArrayList<Producto> = ArrayList()
                    ProductosForLista.value?.forEachIndexed { i, producto ->
                        if (i >= newcount-20 && i < newcount) {
                            productosListPartsToAdd.add(producto)
                        }
                    }
                    ProductosForListaParts.value!!.addAll(productosListPartsToAdd)
                    
                    binding.RecyclerViewHotBuy.adapter?.notifyItemRangeInserted(newcount - 20, 20)
                    
                    //binding.RecyclerViewHotBuy.adapter = Inicio_Producto_Adapter(
                    //    ProductosForListaParts.value!!, OrganizationDollar.value!!, SubirAlCarritoProductos,
                    //    CarritoProductosArray, BajarDelCarritoProductos, event, eventPesables)
                }

            }
        }
    })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文