![](/img/trans.png)
[英]How to know if the user has scrolled to the top or bottom of a listview/scrollview
[英]Enable button after user scrolled to the bottom of ScrollView Android
我想為我的應用程序的一個屏幕實現以下行為。 我有一個帶有 ConstraintLayout 的片段布局,因為它是父級。 在 ConstraintLayout 內部,我有一個帶有嵌套 ConstraintLayout(嵌套 ConstraintLayout 包含 ImageView 和 TextView)的 ScrollView 和 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
期待您的建議。
問候,亞歷克斯
我不知道這是您想要做的,但它應該是解決方案之一。
我認為您可以簡單地在“ScrollView”中添加按鈕,因此當用戶在底部滾動時,用戶將看到該按鈕,而當用戶向上滾動時,用戶也無法按下該按鈕。
下面的布局 .XML 對我有用,將 ScrollView 與 ConstraintLayout 一起使用:
(您可能需要額外的依賴項)
<ScrollView
android:id="@+id/msg_scroll"
android:layout_width="0dp"
android:layout_height="200dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.