RecyclerView在ScrollView上不是trully wrap_content

时间:2017-05-25 23:41:22

标签: android android-layout android-recyclerview android-xml

我有一个ScrollView,,其中包含一个垂直LinearLayout.这是一个地方,我在其中添加一些名为" 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>

部分:

<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>

问题在于RecyclerView有时不是wrap_content。因此,它会产生两种类型的问题(取决于我尝试使用的解决方案类型I)。

  1. 它可以取消滚动(因此它与屏幕高度匹配,我无法将其向下滚动以查看其中的其他项目。)

  2. 它可以滚动嵌套。

  3. 最初的问题是它有嵌套滚动。所以我希望RecyclerViews为简单的垂直LinearLayouts,唯一必须具有滚动效果的是根ScrollView.

    我尝试过什么?

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

      public boolean canScrollVertically(){     返回false; }

    2. 扩展RecyclerView类并覆盖

      @覆盖 public boolean onInterceptTouchEvent(MotionEvent event){     返回false; }

      @覆盖 public boolean onTouchEvent(MotionEvent event){     返回false; }

    3. 在任何地方都可以通过NestedScrolling停用xml

    4. 使用此解决方案覆盖GridLayoutManagerSOLUTION

    5. 合并1-4

2 个答案:

答案 0 :(得分:9)

请勿在{{1​​}}内使用RecyclerViewListView。对于嵌套滚动,您应该使用ScrollView

  

NestedScrollView就像NestedScrollView一样,但它支持充当   新旧版本上的ScrollView滚动父级和子级   Android版默认情况下启用嵌套滚动。

<强> SOLUTION:

1。使用nested作为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>

2. 。对于滚动问题,请使用setNestedScrollingEnabled(false) RecyclerView

答案 1 :(得分:0)

下一步是解决方案: 1.使用NestedScrollView 2.覆盖GridLayoutManager.canScrollVertically():

public boolean canScrollVertically(){ return false; }