首页 > 解决方案 > 用户滚动到 ScrollView Android 底部后启用按钮

问题描述

我想为我的应用程序的一个屏幕实现以下行为。我有一个以 ConstraintLayout 作为父级的片段布局。在 ConstraintLayout 内部,我有一个带有嵌套 ConstraintLayout 的 ScrollView(嵌套 ConstraintLayout 包含 ImageView 和 TextView)和 ScrollView 下方的简单按钮。

我想在用户到达 ScrollView 底部时启用按钮并在用户向上滚动时禁用。

布局如下。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    >

  <ScrollView
      android:id="@+id/scrollableView"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:fillViewport="true"
      app:layout_constraintBottom_toTopOf="@id/elevationShadow"
      app:layout_constraintTop_toBottomOf="@id/appbar">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_marginStart="@dimen/spacing_large"
        android:layout_marginEnd="@dimen/spacing_large"
        android:layout_height="wrap_content">

      <ImageView
          android:id="@+id/user_image"
          android:layout_width="144dp"
          android:layout_height="144dp"
          android:layout_gravity="center"
          android:layout_marginTop="@dimen/spacing_large"
          android:src="@drawable/user_image"
          android:visibility="visible"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          />

      <TextView
          android:id="@+id/heading"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:textSize="@dimen/text_size_xxlarge"
          android:textStyle="bold"
          android:textAlignment="center"
          android:textColor="@color/black_color"
          android:layout_marginTop="@dimen/spacing_large"
          tools:text="Tools text"
          android:textAppearance="?tvptTextAppearanceBody"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/user_image"
          />

      <TextView
          android:id="@+id/content"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="@dimen/spacing_large"
          android:textAlignment="center"
          android:textSize="@dimen/user_info_content_text_size"
          android:textAppearance="?tvptTextAppearanceBody"
          android:textColor="@color/black"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.0"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/heading"
          tools:text="Tools test content"
          />

    </androidx.constraintlayout.widget.ConstraintLayout>

  </ScrollView>

  <View
      android:id="@+id/elevationShadow"
      android:layout_width="match_parent"
      android:layout_height="3dp"
      android:background="@drawable/shadow_elevation"
      app:layout_constraintEnd_toEndOf="parent"
      android:layout_marginBottom="@dimen/user_info_activity_confirm_button_margin"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintBottom_toTopOf="@id/button_confirm"/>

  <com.travelportdigital.android.compasswidget.button.PercentageBasedStateButton
      android:id="@+id/button_confirm"
      style="@style/PrimaryButton"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@android:color/transparent"
      android:layout_marginBottom="@dimen/user_info_activity_confirm_button_margin"
      android:text="@string/user_info_continueButton_title"
      android:textAllCaps="true"
      app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

问题是 ScrollView 内的 TextView 的内容既可以长也可以短。这就是为什么如果内容很长,我必须添加 ScrollView。

通过一小段代码,我能够用一个小小的评论实现我需要的行为。

fun addScrollChangeListener() {
    scrollView.viewTreeObserver
        .addOnScrollChangedListener {
          enableContinueButton(scrollView.getChildAt(0).bottom <= scrollView.height + scrollView.scrollY)
        }
  }

上面的代码适用于内容很长的场景(当用户到达此屏幕时,继续按钮被禁用,当用户滚动到滚动视图的底部时,如果用户向上滚动,它会再次被禁用。

我想更新此逻辑以在用户到达此屏幕并且 ScrollView 内的 TextView 内容很短时启用按钮(这种情况不需要滚动)。

我在谷歌做了一些研究,找不到适合我的解决方案。

在 onViewCreated() 方法中,我添加了当用户到达此屏幕时禁用或启用按钮的逻辑。

enableContinueButton(!isScrollingRequired())

我试过这个实现

  private fun isScrollingRequired(): Boolean {
    val view = scrollView.getChildAt(scrollView.childCount - 1) as View
    val diff = view.bottom - (scrollView.height + scrollView.scrollY)
   return diff != 0
  }

和这个

    return if (child != null) {
      val childHeight = child.height
      scrollView.height <= childHeight + scrollView.paddingTop + scrollView.paddingBottom;
    } else {
      false
    }

但它不起作用,因为 ScrollView 高度和它的子高度始终为 0

期待您的建议。

问候,亚历克斯

标签: androidkotlinandroid-scrollview

解决方案


我不知道这是你想要做的,但它应该是解决方案之一。

我认为您可以简单地在“ScrollView”中添加按钮,这样当用户在底部滚动时,用户将看到该按钮,而当用户向上滚动时,用户也无法按下该按钮。

下面的布局 .XML 对我有用,将 ScrollView 与 ConstraintLayout 一起使用:(
您可能需要额外的依赖项)

<ScrollView
        android:id="@+id/msg_scroll"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:fillViewport="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/infoSumm">
        <!--Display the <ScrollView> under <TextView>"@+id/infoSumm" -->

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/inside_scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/infoDetail"
                android:text=""
                android:layout_marginTop="12dp"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="240dp"
                app:layout_constraintTop_toTopOf="@id/inside_scroll"
                app:layout_constraintStart_toStartOf="parent"
                tools:text="Info Detail"/>


            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_infoClose"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/default_field_spacing"
                android:backgroundTint="@color/colorPrimary"
                android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
                android:textAlignment="center"
                android:textStyle="bold"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:textColor="@color/colorWhite"
                android:text="@string/btn_Close"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                app:cornerRadius="25dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/infoDetail"/>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

推荐阅读