【问题标题】:How to make recyclerview show all item and can not scroll如何使recyclerview显示所有项目并且不能滚动
【发布时间】:2016-12-11 12:16:13
【问题描述】:

我正在制作一个应用程序以在 1 个视图中显示所有产品。我希望 recyclerview 显示所有项目并且不能滚动,只需滚动父视图( ScrollView )。但问题是不能让回收器的高度包裹所有内容。

-->>I want like this

这是我的代码:

    <TextView
        android:text="Best seller:"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView4"
        android:textColor="#3f3f3f"
        android:textSize="18sp" />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:scrollbars="horizontal"
        android:id="@+id/rv_bestSeller"
        android:layout_height="200dp" />

    <TextView
        android:text="New Product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView5"
        android:textSize="18sp"
        android:textColor="#3f3f3f" />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:scrollbars="horizontal"
        android:layout_height="200dp"
        android:id="@+id/rv_newProduct"/>

    <TextView
        android:text="All Product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView6"
        android:textSize="18sp"
        android:textColor="#3f3f3f" />
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rv_allProduct" />
</LinearLayout>
</ScrollView>

-->>But the space is so small

【问题讨论】:

    标签: android scroll android-recyclerview


    【解决方案1】:

    您应该结合使用NestedScrollViewsetNestedScrollingEnabled(false)

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
    
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

      【解决方案2】:

      这样做是不好的做法,如果你这样做是因为标题使用这个库 https://github.com/emilsjolander/StickyListHeaders

      它会给你标题,你不需要里面有列表的滚动视图。

      如果你真的想以这种方式继续使用 NestedScrollView 而不是常规的 Scrollview

      https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html

      这将使您可以根据需要禁用嵌套滚动

      【讨论】:

      • 使用 setNestedScrollingEnabled(false);
      • NestedScrollView 和 setNestedScrollingEnabled(false) 修复了它。竖起大拇指。愿原力与你同在。
      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 2021-10-08
      • 2021-01-19
      • 2016-03-22
      • 1970-01-01
      • 2019-06-15
      • 2018-09-30
      相关资源
      最近更新 更多