【问题标题】:RecyclerView inside of NestedScrollView fires all onBindViewHolder at onceNestedScrollView 内的 RecyclerView 立即触发所有 onBindViewHolder
【发布时间】:2018-12-07 17:39:25
【问题描述】:

我希望将 RecyclerView 作为其子级之一的可滚动屏幕,层次结构如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    ...
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        .../>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                .../>

            <FrameLayout
                .../>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/list_contacts"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:nestedScrollingEnabled="false"/>

            <FrameLayout
                .../>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

注意RecyclerView的所有项目都应该是可见的并且是ImageView、FrameLayout等的兄弟。

在当前的解决方案中,有一个重要问题,onBindViewHolder 会同时为所有项目调用,但我希望它们在出现在屏幕上时被绑定,就像在标准 RecyclerView .我正在做一些实验

android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"

但终究失败了。

我知道其中一种解决方案是实现 heterogeneous recyclerView,但我想避免它。有什么想法吗?

【问题讨论】:

  • 如果您不希望每个视图都预先绑定,则不能使用wrap_content height。 wrap_content height 使视图回收变得不可能。
  • 使用NonScrollRecyclerView这使得在运行时分配recyclerview的高度变得简单。看到这个gist.github.com/abhi08singh/63a010b9ae7fddceec60a533d002e3fc
  • @BenP。好的,你写了如何不实现它。有什么建议如何使用相当复杂的页眉(或/和页脚)放置在列表中的第一个(或最后一个)元素之前的对象列表?

标签: android user-interface android-recyclerview android-nestedscrollview


【解决方案1】:

可能是因为在这种情况下,recyclerview 无法知道自身的高度。 你可以试试:

layoutManager.isAutoMeasureEnabled=true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2017-02-15
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多