简体   繁体   中英

Scrollview over the fixed image/ImageView Android

I've gone through numerous links but no luck. When I scroll (vertical) the form up, it hides under the top image. This is a fixed ImageView (height=200dp). The requirement is that when I scroll up the Freight Forwarding Sea form, it should scroll over the image instead below/under the image. I appreciate for prompt solution. Kind Regards Here is the xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/light_grey"
    tools:context=".fragments.FFSFragment">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/sea_bg">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"/>
    </FrameLayout>
    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="200dp"
        android:scrollbars="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@drawable/rectangle_corners_layout">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/menu_ffs"
                android:textSize="25sp"
                android:textColor="@android:color/black"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"/>
            <TextView
                android:id="@+id/text_cb"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/fill_form"
                android:textSize="16sp"
                android:textColor="@android:color/black"
                android:layout_marginLeft="20dp"
                android:layout_marginBottom="30dp"/>
            <EditText
                android:id="@+id/et_origin"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_origin"

                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_destination"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_destination"
                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_commodity"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_commodity"
                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_weight"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_weight"
                android:inputType="number"
                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_boxes"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_boxes"
                android:inputType="number"
                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_boxe_size"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_boxsize"
                android:background="@drawable/rectangle_corners"/>
            <Button
                android:id="@+id/choose_images_btn"
                style="@style/AppTheme.RoundButtons"
                android:text="@string/choose_images"/>
            <Button
                android:id="@+id/submit_btn"
                style="@style/AppTheme.RoundButtons"
                android:layout_marginBottom="20dp"
                android:text="@string/submit"/>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

The easiest way to do this is by using MotionLayout :

  • First, to sure my code going to work with you implement MotionLayout dependency in your build.gradle(app) :
    implementation "androidx.constraintlayout:constraintlayout:2.1.4"
  • Change the RelativeLayout to MotionLayout , and ScrollView to NestedScrollView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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"
    android:background="#DDD"
    android:orientation="vertical"
    app:layoutDescription="@xml/activity_main_scene">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp" />
    </FrameLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/frameLayout">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:orientation="vertical">
            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/menu_ffs"
                android:textSize="25sp"
                android:textColor="@android:color/black"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"/>
            <TextView
                android:id="@+id/text_cb"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/fill_form"
                android:textSize="16sp"
                android:textColor="@android:color/black"
                android:layout_marginLeft="20dp"
                android:layout_marginBottom="30dp"/>
            <EditText
                android:id="@+id/et_origin"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_origin"

                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_destination"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_destination"
                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_commodity"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_commodity"
                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_weight"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_weight"
                android:inputType="number"
                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_boxes"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_boxes"
                android:inputType="number"
                android:background="@drawable/rectangle_corners"/>
            <EditText
                android:id="@+id/et_boxe_size"
                style="@style/AppTheme.EditTexts"
                android:hint="@string/enter_boxsize"
                android:background="@drawable/rectangle_corners"/>
            <Button
                android:id="@+id/choose_images_btn"
                style="@style/AppTheme.RoundButtons"
                android:text="@string/choose_images"/>
            <Button
                android:id="@+id/submit_btn"
                style="@style/AppTheme.RoundButtons"
                android:layout_marginBottom="20dp"
                android:text="@string/submit"/>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.motion.widget.MotionLayout>
  • Here focus on the line app:layoutDescription="@xml/activity_main_scene" , Android studio going to give you a red error, click at red lamp to show Context Action like in the image below and press at Create xml resource file

在此处输入图像描述

  • Now go to the file res\xml\activity_main_scene and past this code:

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000">
        <KeyFrameSet></KeyFrameSet>
        <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@+id/scroll_view"
            motion:touchAnchorSide="top" />
    </Transition>

    <ConstraintSet android:id="@+id/start"></ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:scrollbars="vertical"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintHorizontal_bias="0.5"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="@+id/frameLayout" />

    </ConstraintSet>
</MotionScene>

I hope this helps , and you can read more about MotionLayout its really helpful in this kind of situation

  • Remove Relative layout, use FrameLayout instead

  • Another issue is you are providing 200dp paddingTop to Scrollview which stays even when you scroll the view and cause the issue

  • You can refer to this too!

     <FrameLayout 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/nestedScrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/light_grey" tools:context=".fragments.FFSFragment"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="fitXY" android:src="@drawable/sea_bg" /> <ScrollView android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="200dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/rectangle_corners_layout"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/menu_ffs" android:textSize="25sp" android:textColor="@android:color/black" android:layout_marginLeft="20dp" android:layout_marginTop="20dp"/> <TextView android:id="@+id/text_cb" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/fill_form" android:textSize="16sp" android:textColor="@android:color/black" android:layout_marginLeft="20dp" android:layout_marginBottom="30dp"/> <EditText android:id="@+id/et_origin" style="@style/AppTheme.EditTexts" android:hint="@string/enter_origin" android:background="@drawable/rectangle_corners"/> <EditText android:id="@+id/et_destination" style="@style/AppTheme.EditTexts" android:hint="@string/enter_destination" android:background="@drawable/rectangle_corners"/> <EditText android:id="@+id/et_commodity" style="@style/AppTheme.EditTexts" android:hint="@string/enter_commodity" android:background="@drawable/rectangle_corners"/> <EditText android:id="@+id/et_weight" style="@style/AppTheme.EditTexts" android:hint="@string/enter_weight" android:inputType="number" android:background="@drawable/rectangle_corners"/> <EditText android:id="@+id/et_boxes" style="@style/AppTheme.EditTexts" android:hint="@string/enter_boxes" android:inputType="number" android:background="@drawable/rectangle_corners"/> <EditText android:id="@+id/et_boxe_size" style="@style/AppTheme.EditTexts" android:hint="@string/enter_boxsize" android:background="@drawable/rectangle_corners"/> <Button android:id="@+id/choose_images_btn" style="@style/AppTheme.RoundButtons" android:text="@string/choose_images"/> <Button android:id="@+id/submit_btn" style="@style/AppTheme.RoundButtons" android:layout_marginBottom="20dp" android:text="@string/submit"/> </LinearLayout> </LinearLayout> </ScrollView> </FrameLayout>

I Hope this answer Helps You!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM