![UI](https://istack.dev59.com/5cYhz.webp)
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed" >
<CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<!-- My Views Goes there -->
</CardView
<android.support.v7.widget.Toolbar
android:id="@+id/flexible.example.toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@null"
app:layout_collapseMode="pin"
style="@style/ToolBarWithNavigationBack"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RecyclerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
></RecyclerView>
</android.support.design.widget.CoordinatorLayout>
P.S: 我已删除无关代码,请勿提及我
这种方法是正确的,但是当CardView
高度超过屏幕高度时,它的内容被AppBarLayout
忽略,用户无法看到。
2. 使用NestedScrollView
我将CardView
和RecyclerView
放在NestedScrollView
中。但问题是,当用户滚动到RecyclerView底部,然后向上滚动时,滑动会变得卡顿和错误,并且停止在某个位置,用户必须不断滚动才能回到顶部!
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/nested_scrollbar_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.CardView
android:id="@+id/flexible.example.cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/post_card_backgroind"
app:cardCornerRadius="0dp"
app:cardElevation="0dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/four"
android:layout_marginEnd="@dimen/four"
android:layout_marginLeft="@dimen/four"
android:layout_marginRight="@dimen/four"
android:layout_marginStart="@dimen/four"
android:layout_marginTop="@dimen/four"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
如何解决这个问题?!我不想使用为recyclerview创建头的适配器,因为其中一些会导致性能问题。
答案
将RecyclerView
放在NestedScrollView
中,如2所示,并应用.setNestedScrollingEnabled
并将其设置为false
CardView
固定在顶部还是像RecyclerView
中的其他项一样简单滚动? - RehanCardView
像RecyclerView
中的其他项一样滚动)。你能发布第二个布局,其中你将CardView
和RecyclerView
放在NestedScrollView
中吗? - Rehan