I’m working on an Android app in Android Studio, and I’m facing a challenge with fragment management. Specifically, I have a layout with two fragments stacked vertically. The first fragment occupies the top portion of the screen, and the second fragment is below it.
<?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:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
tools:context=".ui.account.AccountFragment">
<FrameLayout
android:id="@+id/profit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<FrameLayout
android:id="@+id/transaction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintTop_toBottomOf="@id/profit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
What I want to achieve is to make a button appear in the second fragment only after the user has scrolled enough to hide the first fragment. In other words, when the user scrolls down and the first fragment disappears from view, I want a button to dynamically appear in the fragment general.
I’ve tried exploring various methods, such as implementing OnScrollListener or using NestedScrollView, but I haven’t been able to figure out how to detect when the first fragment is no longer visible due to scrolling and trigger the appearance of the button in the second fragment.
Could someone please provide guidance or a code example on how to achieve this behavior? Any help or suggestions would be greatly appreciated.
Thank you in advance.