Скрыть панель действий при прокрутке через ReyclerView [Android Studio] - PullRequest
0 голосов
/ 18 апреля 2020

Как скрыть панель действий при прокрутке через RecyclerView в Android. Вот код, который я использую:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapp.activities.Profile_Page">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="87dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toTopOf="@+id/notifications_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:fontFamily="@font/roboto_medium"
            android:text="Notifications"
            android:textAlignment="center"
            android:textColor="#FFFFFF"
            android:textSize="27sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"></TextView>
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="87dp"
        android:padding="10dp"
        android:id="@+id/notifications_view"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        ></androidx.recyclerview.widget.RecyclerView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Чтобы инициализировать ActionBar, я добавил следующий код в файл Java:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setElevation(0);

При прокрутке страницы Какие способы, с помощью которых я могу скрыть панель действий? Я использовал макет «Constraint», чтобы отображать заголовок «Действия» по-своему и чтобы он выглядел красиво.

Когда пользователь прокручивает, панель действий и ConstraintLayout должны скрываться.

Ответы [ 2 ]

1 голос
/ 18 апреля 2020

Вам необходимо обернуть макет ограничения в AppBarLayout и добавить layout_scrollFlags в макет ограничения следующим образом:

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp"
    >
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="87dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toTopOf="@+id/notifications_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:fontFamily="@font/roboto_medium"
            android:text="Notifications"
            android:textAlignment="center"
            android:textColor="#FFFFFF"
            android:textSize="27sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"></TextView>
    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
0 голосов
/ 18 апреля 2020

Вам необходимо использовать CollapsingToolbarLayout и NestedScrollView. У меня есть часть кода. Там я написал не полностью свернутую панель действий.

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/cityBlock"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/sky_blue"
        android:visibility="gone"
        tools:visibility="visible">


        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/cityAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/cityToolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:collapsedTitleGravity="start"
                app:collapsedTitleTextAppearance="@style/CollapsedAppBar"
                app:contentScrim="@color/sky_blue"
                app:expandedTitleGravity="center|top"
                app:expandedTitleTextAppearance="@style/ExpandedAppBar"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                app:titleEnabled="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|bottom"
                    android:layout_marginTop="@dimen/huge_margin"
                    android:layout_marginBottom="@dimen/default_margin"
                    android:gravity="center"
                    android:orientation="vertical"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.5">

                    <TextView
                        android:id="@+id/countryTitle"
                        style="@style/TextAppearance.MaterialComponents.Subtitle1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@android:color/white"
                        tools:text="Russia" />

                    <TextView
                        android:id="@+id/weatherDate"
                        style="@style/TextAppearance.MaterialComponents.Subtitle1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@android:color/white"
                        tools:text="20.20.20" />

                    <ImageView
                        android:id="@+id/weatherBigImage"
                        android:layout_width="56dp"
                        android:layout_height="56dp"
                        android:layout_margin="@dimen/mini_margin"
                        tools:src="@drawable/ic_test_icon_2" />

                    <TextView
                        android:id="@+id/mainTemp"
                        style="@style/TemperatureTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        tools:text="10\u00B0" />

                    <TextView
                        android:id="@+id/minMaxTempFull"
                        style="@style/MinMaxTemperatureTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/default_margin"
                        tools:text="@string/min_max_temp_pattern" />

                </LinearLayout>

                <androidx.appcompat.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap" />

            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/cityDetailSwipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
            app:layout_scrollFlags="scroll|enterAlways">

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

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

                    <androidx.cardview.widget.CardView
                        android:id="@+id/detailBlock"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        app:cardCornerRadius="15dp"
                        app:cardElevation="5dp"
                        app:cardUseCompatPadding="true"
                        app:contentPaddingBottom="@dimen/default_margin"
                        app:contentPaddingTop="@dimen/default_margin"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">

                        <androidx.constraintlayout.widget.ConstraintLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/default_margin">

                            <FrameLayout
                                android:id="@+id/humidityBlock"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="@dimen/default_margin"
                                app:layout_constraintBottom_toTopOf="@id/humidityTitle"
                                app:layout_constraintEnd_toStartOf="@id/windBlock"
                                app:layout_constraintStart_toStartOf="parent"
                                app:layout_constraintTop_toTopOf="parent"
                                app:layout_constraintWidth_percent="30">

                                <ImageView
                                    android:id="@+id/humidityImage"
                                    android:layout_width="36dp"
                                    android:layout_height="36dp"
                                    android:layout_gravity="center"
                                    android:layout_marginEnd="@dimen/mini_margin"
                                    android:src="@drawable/ic_humidity" />

                            </FrameLayout>

                            <FrameLayout
                                android:id="@+id/windBlock"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="@dimen/default_margin"
                                app:layout_constraintBottom_toTopOf="@id/windSpeedTitle"
                                app:layout_constraintEnd_toEndOf="parent"
                                app:layout_constraintStart_toEndOf="@id/humidityBlock"
                                app:layout_constraintTop_toTopOf="parent"
                                app:layout_constraintWidth_percent="70">

                                <ImageView
                                    android:id="@+id/windImage"
                                    android:layout_width="36dp"
                                    android:layout_height="36dp"
                                    android:layout_gravity="center"
                                    android:layout_marginEnd="@dimen/mini_margin"
                                    android:src="@drawable/ic_wind" />
                            </FrameLayout>

                            <TextView
                                android:id="@+id/humidityTitle"
                                style="@style/ValuesHint"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginEnd="@dimen/mini_margin"
                                android:text="@string/humidity_hint"
                                app:layout_constraintBottom_toTopOf="@id/humidityValue"
                                app:layout_constraintEnd_toStartOf="@id/windSpeedTitle"
                                app:layout_constraintStart_toStartOf="@id/humidityBlock"
                                app:layout_constraintTop_toBottomOf="@id/humidityBlock" />

                            <TextView
                                android:id="@+id/humidityValue"
                                style="@style/ValuesHint"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/mini_margin"
                                android:text="@string/percentage_pattern"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintStart_toStartOf="@id/humidityTitle"
                                app:layout_constraintTop_toBottomOf="@id/humidityTitle" />

                            <View
                                android:id="@+id/mainDivider"
                                android:layout_width="1dp"
                                android:layout_height="0dp"
                                android:background="@color/light_gray"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintEnd_toStartOf="@id/windSpeedTitle"
                                app:layout_constraintStart_toEndOf="@id/humidityTitle"
                                app:layout_constraintTop_toTopOf="parent" />

                            <TextView
                                android:id="@+id/windSpeedTitle"
                                style="@style/ValuesHint"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginEnd="@dimen/mini_margin"
                                android:text="@string/wind_speed_title"
                                android:textColor="@android:color/holo_blue_dark"
                                app:layout_constraintBottom_toTopOf="@id/windSpeedValue"
                                app:layout_constraintEnd_toStartOf="@id/divider2"
                                app:layout_constraintStart_toStartOf="@id/windBlock"
                                app:layout_constraintTop_toBottomOf="@id/windBlock" />

                            <TextView
                                android:id="@+id/windSpeedValue"
                                style="@style/ValuesHint"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:layout_alignParentEnd="true"
                                android:layout_marginTop="@dimen/mini_margin"
                                android:textColor="@android:color/holo_blue_dark"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintStart_toStartOf="@id/windSpeedTitle"
                                app:layout_constraintTop_toBottomOf="@id/windSpeedTitle"
                                tools:text="@string/wind_speed_pattern" />

                            <View
                                android:id="@+id/divider2"
                                android:layout_width="1dp"
                                android:layout_height="0dp"
                                android:layout_marginTop="@dimen/mini_margin"
                                android:layout_marginEnd="@dimen/mini_margin"
                                android:background="@color/light_gray"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintEnd_toStartOf="@id/windDirectionTitle"
                                app:layout_constraintStart_toEndOf="@id/windSpeedTitle"
                                app:layout_constraintTop_toBottomOf="@id/humidityBlock" />

                            <TextView
                                android:id="@+id/windDirectionTitle"
                                style="@style/ValuesHint"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginEnd="@dimen/mini_margin"
                                android:text="@string/wind_direction_title"
                                android:textColor="@android:color/holo_blue_dark"
                                app:layout_constraintBottom_toTopOf="@id/windDirectionValue"
                                app:layout_constraintEnd_toEndOf="parent"
                                app:layout_constraintStart_toEndOf="@id/divider2"
                                app:layout_constraintTop_toTopOf="@id/windSpeedTitle" />

                            <TextView
                                android:id="@+id/windDirectionValue"
                                style="@style/ValuesHint"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/mini_margin"
                                android:textColor="@android:color/holo_blue_dark"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintStart_toStartOf="@id/windDirectionTitle"
                                app:layout_constraintTop_toBottomOf="@id/windDirectionTitle"
                                tools:text="NorthEast" />

                        </androidx.constraintlayout.widget.ConstraintLayout>

                    </androidx.cardview.widget.CardView>

                    <TextView
                        android:id="@+id/hourlyTitle"
                        style="@style/FragmentsTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/default_margin"
                        android:text="@string/hourly_title"
                        app:layout_constraintBottom_toTopOf="@id/hourlyFragment"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/detailBlock" />

                    <fragment
                        android:id="@+id/hourlyFragment"
                        android:name="com.pesiik.goodbadweather.ui.forecast.ForecastFragment"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/default_margin"
                        android:tag="@string/hourly_fragment_tag"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/hourlyTitle" />

                    <TextView
                        android:id="@+id/dailyTitle"
                        style="@style/FragmentsTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/default_margin"
                        android:text="@string/daily_title"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/hourlyFragment" />

                    <fragment
                        android:id="@+id/dailyFragment"
                        android:name="com.pesiik.goodbadweather.ui.forecast.ForecastFragment"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/default_margin"
                        android:layout_marginBottom="32dp"
                        android:tag="@string/daily_fragment_tag"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/dailyTitle" />

                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.core.widget.NestedScrollView>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/emptyCityBlockResfreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/emptyCityTitle"
                style="@style/CityTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_margin="@dimen/default_margin"
                android:layout_marginTop="100dp"
                android:text="@string/empty_city" />

            <ImageView
                android:layout_width="56dp"
                android:layout_height="150dp"
                android:layout_below="@id/emptyCityTitle"
                android:layout_centerHorizontal="true"
                android:src="@drawable/ic_arrow_pointing_down" />
        </RelativeLayout>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...