简体   繁体   English

如何滚动整个页面,Scrollview中的Recyclerview

[英]How to scroll whole page, Recyclerview inside of Scrollview

i am trying to implement same structure in video but my activity not scrolling down.我正在尝试在视频中实现相同的结构,但我的活动没有向下滚动。 Recyclerview scrolls but i want to scroll whole page like in video Recyclerview 滚动,但我想像在视频中一样滚动整个页面

I am trying to do this one我正在尝试做这个

My design我的设计

<ScrollView>
    
    <androidx.constraintlayout.widget.ConstraintLayout>
 
    ...
    ...
    ...
    
    
    <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/x"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

I solved my problem with this example structure.我用这个示例结构解决了我的问题。 Note: if you use ScrollView instead of NestedScrollView, components slide a bit different注意:如果你使用 ScrollView 而不是 NestedScrollView,组件滑动会有点不同

Final result最后结果

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/base"
    android:clickable="true"
    android:orientation="vertical"
    tools:context=".Fragment.BasketFragment">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb_BasketFragment"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/white"
        android:elevation="4dp"
        app:title="@string/title_basket"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        app:titleTextColor="@color/black" />


    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:overScrollMode="never">

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

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/acb_FragmentBasket"
                android:layout_width="match_parent"
                android:layout_height="42dp"
                android:layout_marginHorizontal="30dp"
                android:layout_marginTop="26dp"
                android:background="@drawable/custom_background_2"
                android:text="@string/continue_shopping"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="16sp"
                app:itemRippleColor="@null" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_FragmentBasket"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>


    </androidx.core.widget.NestedScrollView>


</LinearLayout>

add this attribute to your RecyclerView将此属性添加到您的 RecyclerView

android:nestedScrollingEnabled="false"

if it doesn't work, use NestedScrollView instead of ScrollView如果它不起作用,请使用 NestedScrollView 而不是 ScrollView

You need to set your RecyclerView height as wrap_content , and put all your xml of your activity inside a ScrollView to get the result that you want.您需要将RecyclerView高度设置为wrap_content ,并将活动的所有 xml 放入ScrollView以获得所需的结果。

Your Xml should look like this :您的 Xml 应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:adjustViewBounds="true"
            tools:srcCompat="@tools:sample/backgrounds/scenic" />

        <!-- Your Ui Here: The ImageView is just for test -->

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/x"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/imageView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:itemCount="100"
            />
        
    </androidx.constraintlayout.widget.ConstraintLayout>
    
</ScrollView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM