RecyclerView在ScrollView上不是trully wrap_content

M. *_*ich 4 android android-layout android-xml android-recyclerview

我有一个ScrollView,包含垂直LinearLayout.的地方这是一个地方,我添加了一些名为"Section"的视图."Section" LinearLayout,包含一个TextView和`RecyclerView.

<ScrollView
    android:id="@+id/main_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/sections_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

部分:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="@dimen/activity_starred_title_textsize" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

问题是有时RecyclerView并非真正的问题wrap_content.因此,它会产生两种类型的问题(取决于我尝试使用的解决方案类型).

  1. 它可以是不可滚动的(因此它匹配屏幕高度,我无法向下滚动以查看其中的其余项目).

  2. 它可以滚动嵌套.

最初的问题是它有嵌套滚动.所以我想要RecyclerViews简单的垂直LinearLayouts,唯一必须具有滚动效果的是rootScrollView.

我试过做什么?

  1. 扩展GridLayoutManager和覆盖canScrollVertically().方法:

    public boolean canScrollVertically(){return false; }

  2. 扩展RecyclerView类和覆盖

    @Override public boolean onInterceptTouchEvent(MotionEvent event){return false; }

    @Override public boolean onTouchEvent(MotionEvent event){return false; }

  3. 禁止NestedScrolling通过xml无处不在,它是可能的.

  4. GridLayoutManager使用此解决方案覆盖:解决方案

  5. 结合1-4

Fer*_*med 10

不要使用RecyclerViewListView在里面ScrollView.对于嵌套滚动,您应该使用NestedScrollView.

NestedScrollView就像ScrollView,但它支持nested在新旧版本的Android上同时作为滚动父级和子级.默认情况下启用嵌套滚动.

解:

1.而不是使用ScrollView,NestedScrollView作为你的Section部分(RecyclerView和其他Views)的容器使用.

<android.support.v4.widget.NestedScrollView
    android:id="@+id/main_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/sections_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Your Section:: RecyclerView and other Views -->

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

2.滚动问题使用setNestedScrollingEnabled(false)到您的RecyclerView.