如何在NestedScrollView内使用RecyclerView ? 设置适配器后,RecyclerView内容不可见。

更新布局代码。

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/keyline_1">

    </RelativeLayout>

    <View
        android:id="@+id/separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#e5e5e5" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/conversation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

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

当前回答

不要在NestedScrollView中使用recyclerView。这可能会导致级联问题! 我建议使用ItemViewTypes在RecyclerView处理多种类型的视图。 只需添加一个RecyclerView与match_parent宽度和高度。然后在你的recyclerViewAdapter覆盖getItemViewType和使用position处理什么布局被膨胀。之后,你可以使用onBindViewHolder方法来处理你的视图持有者。

https://stacklearn.ir

其他回答

你可以使用android:fillViewport="true"来让NestedScrollView测量RecyclerView。RecyclerView将填充剩余的高度。所以如果你想滚动NestScrollView,你可以设置RecyclerView的minHeight。

您需要使用23.2.0(或)以上版本的支持库 和RecyclerView高度将wrap_content。 recyclerView.setNestedScrollingEnabled(假)

但是这样循环模式就行不通了。(即所有的视图将被一次加载,因为wrap_content需要完整的RecyclerView的高度,所以它将一次绘制所有的子视图。没有视图将被回收)。除非确实需要,否则尽量不要使用这种模式。尝试使用viewType并添加所有其他需要滚动到RecyclerView的视图,而不是在Scrollview中使用RecyclerView。性能影响将非常大。

为了让它变得简单"它就像所有子视图的LinearLayout一样"

有很多很好的答案。关键是你必须将nestedScrollingEnabled设置为false。如上所述,你可以在java代码中做到这一点:

mRecyclerView.setNestedScrollingEnabled(false);

但你也有机会在xml代码中设置相同的属性(android:nestedScrollingEnabled="false"):

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

下面是我用来避免滚动问题的代码:

mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.getLayoutManager().setAutoMeasureEnabled(true);
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(false);

我在NestedScrollView里面有Viewpager和RecyclerView。在添加以下行之后

recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);

我解决了滚动缓慢和滚动延迟的问题。