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

时间:2020-10-17 11:14:20

标签: android kotlin android-scrollview

我想在我的应用程序的一个屏幕上实现以下行为。 我有一个以ConstraintLayout为父元素的片段布局。在ConstraintLayout内部,我有一个带有嵌套ConstraintLayout(嵌套的ConstraintLayout包含ImageView和TextView)的ScrollView,以及位于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>

问题在于ScrollView内部的TextView内容可以长短。因此,如果内容很长,我必须添加ScrollView。

通过一小段代码,我只需一点点便能实现所需的行为。

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

并且上面的代码对于内容很长的情况(当用户到达此屏幕时,“继续”按钮被禁用,并且当用户滚动到滚动视图的底部时变为启用,如果用户向上滚动,则再次变为禁用)的情形适用。

我想更新此逻辑以在用户到达此屏幕并且ScrollView中的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

期待您的建议。

关于, 亚历克斯

0 个答案:

没有答案