如何滚动整个页面,在滚动视图中回收视图

  • 本文关键字:滚动 视图 何滚动 android
  • 更新时间 :
  • 英文 :


我试图在视频中实现相同的结构,但我的活动不向下滚动。循环视图滚动,但我想滚动整个页面,像在视频

我正在尝试做这个

我的设计

<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>
我用这个例子结构解决了我的问题。注意:如果你使用ScrollView而不是NestedScrollView,组件滑动会有点不同

最终结果

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

将此属性添加到您的RecyclerView

android:nestedScrollingEnabled="false"

如果不工作,使用NestedScrollView代替ScrollView

您需要将您的RecyclerView高度设置为wrap_content,并将您的活动的所有xml放在ScrollView内以获得您想要的结果。

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

最新更新