用户滚动到 Android ScrollView 底部后启用按钮

3
我希望在我的应用程序中的一个屏幕上实现以下行为。 我有一个片段布局,其父级为ConstraintLayout。在ConstraintLayout中,我有一个ScrollView,其中包含嵌套的ConstraintLayout(嵌套的ConstraintLayout包含ImageView和TextView),以及位于ScrollView下方的简单Button。
当用户滚动到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>

问题是TextView中的内容可能既长又短,因此如果内容很长,我就必须添加ScrollView。

通过简短的代码片段,我能够实现所需的行为,并有一个小注释。

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

上述代码在内容较长时的场景下运行良好(当用户到达此屏幕时,“继续”按钮被禁用,当用户滚动到滚动视图底部时,如果用户向上滚动,它就会再次被禁用。)

我想更新这个逻辑,使得当用户到达此屏幕并且滚动视图中TextView的内容较短时(不需要滚动),按钮变为可用。

我在Google上进行了一些研究,但没有找到适合我的解决方案。

在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

期待您的建议。

此致, Alex


问题已经解决。这篇帖子中的答案对我帮助很大。https://dev59.com/KWMl5IYBdhLWcg3wOU5g#57876872 我不得不像评论作者建议的那样在onActivityCreated方法中使用GlobalLayoutListener。 - Alexandru Iachimov
注意:onGlobalLayout不能保证在视图具有尺寸的时候被调用:https://cheesecakelabs.com/blog/understanding-android-views-dimensions-set/ - user2891462
1个回答

0

我不知道这是否是你想要做的,但这应该是其中一种解决方案。

我认为你可以简单地在“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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接