Почему при переходе экрана в состоянии ошибки nestedScrollView скролит экран немного вверх?

На моем экране есть много CustomViews которые в себе содержат TextView для вывода ошибки. По умолчанию эти textView в состоянии gone. Но когда экран уходит в состояние ошибки - все эти customView меняют visibility для TextView c ошибкой на visible. Ошибки отрисовываются под вьюшками и занимают место на экране. От этого экран скролится вверх. Как это можно решить?

введите сюда описание изображения введите сюда описание изображения

<com.umbrella.custom_view.common.InterceptableCoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    style="@style/RootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.umbrella.custom_view.order.HeaderView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/create_advertising"
            android:textAppearance="@style/Header.Header2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/selectCarTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2_5"
            android:text="@string/select_car"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/carsList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/grid_2_5"
            android:clipToPadding="false"
            android:orientation="horizontal"
            android:overScrollMode="never"
            android:paddingRight="@dimen/grid_2"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintTop_toBottomOf="@id/selectCarTitle" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/carsListErrorMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/grid_2"
            android:layout_marginBottom="@dimen/grid_1"
            android:textAppearance="@style/Error.Error2"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@+id/addCar" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/noCar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/grid_6_75"
            android:text="@string/no_auto"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/selectCarTitle" />

        <com.umbrella.custom_view.create_adverisement.AddButtonWithText
            android:id="@+id/addCar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_17"
            android:text="@string/add_auto"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/selectCarTitle" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/directionTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_4"
            android:text="@string/direction"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/addCar" />

        <RadioGroup
            android:id="@+id/directionView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:orientation="vertical"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/directionTitle">

            <RadioButton
                android:id="@+id/radioCity"
                style="@style/RadioButton.Default"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/within_city"
                android:textAppearance="@style/Body.Body2" />

            <RadioButton
                android:id="@+id/radioIntercity"
                style="@style/RadioButton.Default"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/intercity"
                android:textAppearance="@style/Body.Body2" />

        </RadioGroup>

        <com.umbrella.custom_view.order.ViewVerticalRouteEditing
            android:id="@+id/intercityDirection"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginEnd="@dimen/grid_2"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/directionView" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/phoneTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_4"
            android:text="@string/contact_number"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/intercityDirection" />

        <com.umbrella.custom_view.common.SpinnerWithHint
            android:id="@+id/phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_2"
            android:hint="@string/edit_phone_number"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/phoneTitle" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/cargoWeightTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:text="@string/cargo_weight"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/phone" />

        <com.umbrella.custom_view.common.InputTextViewWithHint
            android:id="@+id/cargoWeight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_2"
            android:hint="@string/edit_cargo_weight"
            android:inputType="numberDecimal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/cargoWeightTitle" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/cargoTypeTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:text="@string/cargo_type"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/cargoWeight" />

        <com.umbrella.custom_view.create_adverisement.SideNavigationButtonWithSelectedValues
            android:id="@+id/cargoTypes"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_0_5"
            android:hint="@string/edit_cargo_type"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cargoTypeTitle" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/rateTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:text="@string/rate"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/cargoTypes" />

        <com.umbrella.custom_view.common.SpinnerWithHint
            android:id="@+id/rateType"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginEnd="@dimen/grid_2"
            app:layout_constraintBottom_toBottomOf="@+id/rate"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/rate"
            app:layout_constraintTop_toTopOf="@+id/rate" />

        <com.umbrella.custom_view.common.InputTextViewWithHint
            android:id="@+id/rate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_2"
            android:hint="@string/rate_hint"
            android:inputType="number"
            android:maxLength="12"
            app:layout_constraintEnd_toStartOf="@id/rateType"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/rateTitle" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/workPeriodTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:text="@string/work_period"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/rate" />

        <com.umbrella.custom_view.create_adverisement.WorkingDaysPicker
            android:id="@+id/startWorkingDaysPicker"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_0_5"
            app:layout_constraintEnd_toStartOf="@id/datePickerDivider"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/workPeriodTitle" />

        <com.umbrella.custom_view.create_adverisement.WorkingDaysPicker
            android:id="@+id/endWorkingDaysPicker"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/datePickerDivider"
            app:layout_constraintTop_toBottomOf="@id/workPeriodTitle" />

        <View
            android:id="@+id/datePickerDivider"
            android:layout_width="@dimen/grid_2"
            android:layout_height="@dimen/grid_0_125"
            android:layout_marginStart="@dimen/grid_1_75"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_1_75"
            android:background="@color/secondary"
            app:layout_constraintBottom_toBottomOf="@+id/startWorkingDaysPicker"
            app:layout_constraintEnd_toStartOf="@id/endWorkingDaysPicker"
            app:layout_constraintStart_toEndOf="@id/startWorkingDaysPicker"
            app:layout_constraintTop_toTopOf="@+id/startWorkingDaysPicker" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/workingHoursTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:text="@string/working_hours"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/startWorkingDaysPicker" />

        <com.umbrella.custom_view.create_adverisement.WorkingHoursPicker
            android:id="@+id/startWorkingHoursPicker"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_0_5"
            app:layout_constraintEnd_toStartOf="@id/hoursPickerDivider"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/workingHoursTitle" />

        <com.umbrella.custom_view.create_adverisement.WorkingHoursPicker
            android:id="@+id/endWorkingHoursPicker"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/hoursPickerDivider"
            app:layout_constraintTop_toBottomOf="@id/workingHoursTitle" />

        <View
            android:id="@+id/hoursPickerDivider"
            android:layout_width="@dimen/grid_2"
            android:layout_height="@dimen/grid_0_125"
            android:layout_marginStart="@dimen/grid_1_75"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_1_75"
            android:background="@color/secondary"
            app:layout_constraintBottom_toBottomOf="@+id/startWorkingHoursPicker"
            app:layout_constraintEnd_toStartOf="@id/endWorkingHoursPicker"
            app:layout_constraintStart_toEndOf="@id/startWorkingHoursPicker"
            app:layout_constraintTop_toTopOf="@+id/startWorkingHoursPicker" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/paymentTypeTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:text="@string/payment_type"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/startWorkingHoursPicker" />

        <com.umbrella.custom_view.common.SpinnerWithHint
            android:id="@+id/paymentMethod"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/paymentTypeTitle" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/commentTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_2"
            android:text="@string/comment"
            android:textAppearance="@style/Subtext.Subtext2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/paymentMethod" />

        <com.umbrella.custom_view.common.InputTextViewWithHint
            android:id="@+id/comment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/grid_2"
            android:layout_marginTop="@dimen/grid_0_5"
            android:layout_marginEnd="@dimen/grid_2"
            android:hint="@string/edit_comment"
            android:inputType="textMultiLine"
            android:maxLength="255"
            android:minHeight="@dimen/grid_15_5"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/commentTitle" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/createAdvertiseError"
            style="@style/Error.Error2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/grid_1"
            android:text="@string/something_went_wrong"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@+id/createAdvertise"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

            <com.umbrella.custom_view.common.ButtonWithLoader
                android:id="@+id/createAdvertise"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/grid_2"
                android:layout_marginTop="@dimen/grid_8"
                android:layout_marginEnd="@dimen/grid_2"
                android:layout_marginBottom="@dimen/grid_5"
                android:text="@string/publish"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/comment" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</com.umbrella.custom_view.common.InterceptableCoordinatorLayout>

InterceptableCoordinatorLayout используется для реализации очистки фокуса с Veiw при клике по области экрана

    class InterceptableCoordinatorLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : CoordinatorLayout(context, attrs, defStyleAttr) {

    private val onDispatchTouchEventListeners: MutableSet<OnDispatchTouchEventListener> = LinkedHashSet()

    init {
        isFocusable = true
        isFocusableInTouchMode = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            isFocusedByDefault = true
        }
    }

    override fun onDetachedFromWindow() {
        ViewCompat.setOnApplyWindowInsetsListener(this, null)
        super.onDetachedFromWindow()
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        requestFocus()
    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        for (listener in onDispatchTouchEventListeners) {
            if (listener.dispatchTouchEvent(this, event)) {
                return true
            }
        }
        return super.dispatchTouchEvent(event)
    }

    fun addOnDispatchTouchEventListener(listener: OnDispatchTouchEventListener) {
        onDispatchTouchEventListeners.add(listener)
    }

    fun removeOnDispatchTouchEventListener(listener: OnDispatchTouchEventListener) {
        onDispatchTouchEventListeners.remove(listener)
    }
    
}

Ответы (0 шт):