смотри я делаю приложение на android studio на java у меня есть xml в котом есть 2…
- User
смотри я делаю приложение на android studio на java у меня есть xml в котом есть 2 кнопки и recycler view можешь добавить toolbar в котором будут title фрагмента 2 кнопки и textview при скроле toolbar должен скрываться <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@drawable/bg" tools:context=".Fragments.InventoryFragment">
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:fontFamily="@font/bold" android:text="Инвентарь" android:textColor="@color/white" android:textSize="28sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.google.android.material.button.MaterialButton android:id="@+id/selectAllButton" android:layout_width="100dp" android:layout_height="50dp" android:layout_marginStart="20dp" android:text="Выбрать всё" android:fontFamily="@font/normal" android:textColor="@color/white" android:textSize="10sp" app:backgroundTint="@android:color/transparent" app:cornerRadius="10dp" app:strokeColor="@color/white" app:strokeWidth="1dp" android:layout_marginTop="10dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <com.google.android.material.button.MaterialButton android:id="@+id/sellAllButton" android:layout_width="100dp" android:layout_height="50dp" android:layout_marginTop="10dp" android:layout_marginEnd="20dp" android:fontFamily="@font/normal" android:text="Продать всё" android:textColor="@color/white" android:textSize="10sp" app:backgroundTint="@android:color/transparent" app:cornerRadius="10dp" app:strokeColor="@color/white" app:strokeWidth="1dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <TextView android:id="@+id/totalSoldPriceTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:fontFamily="@font/normal" android:gravity="center" android:textColor="@color/white" android:textSize="16sp" app:layout_constraintEnd_toStartOf="@+id/sellAllButton" app:layout_constraintStart_toEndOf="@+id/selectAllButton" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/inventorRecyclerView" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="50dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/selectAllButton" /></androidx.constraintlayout.widget.ConstraintLayout>
- Assistant
Для того чтобы добавить Toolbar с возможностью скрытия при скролле, вам понадобится использовать
AppBarLayoutиCollapsingToolbarLayoutиз библиотеки Design Support. В этом примере мы также будем использоватьCoordinatorLayoutв качестве родительского макета, потому что он предоставляет возможность взаимодействия между дочерними элементами (например, RecyclerView и AppBarLayout).Вот как вы можете добавить Toolbar с желаемым поведением в ваш XML-макет:
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" xmlns:tools="http://schemas.android.com/tools" tools:context=".Fragments.InventoryFragment"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:contentScrim="?attr/colorPrimary"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> <!-- Ваш заголовок, кнопки и TextView могут пойти сюда --> </androidx.appcompat.widget.Toolbar> <!-- Другие элементы интерфейса, которые должны скрываться при прокрутке вверх --> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/inventorRecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>Обратите внимание на несколько новых атрибутов:
app:layout_scrollFlags="scroll|exitUntilCollapsed": Это говорит о том, что CollapsingToolbarLayout можно прокрутить вниз и он будет скрываться до тех пор, пока не достигнет минимального размера (в нашем случае, высоты Toolbar).app:layout_collapseMode="pin": Это сообщает Toolbar оставаться "прикрепленной" на месте, когда CollapsingToolbarLayout сжимается.app:layout_behavior="@string/appbar_scrolling_view_behavior": Используется для RecyclerView для поддержки поведения прокрутки с AppBarLayout.
Вы можете кастомизировать Toolbar, добавив заголовок, кнопки и TextView, которые вы хотите использовать, непосредственно в тег
<Toolbar>.Не забудьте добавить необходимые зависимости Material Design в ваш файл
build.gradleесли они ещё не добавлены:implementation 'com.google.android.material:material:<версия>'Где
<версия>- это актуальная версия библиотеки Material Components для Android на момент вашей разработки. - User
покажи как должен выглядеть полный xml со всеми моими кнопками и замени свои кавычки на эти "" <androidx.coordinatorlayout.widget.CoordinatorLayout 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=“@drawable/bg” xmlns:tools=“http://schemas.android.com/tools” tools:context=“.Fragments.InventoryFragment”>
<com.google.android.material.appbar.AppBarLayout android:layout_width=“match_parent” android:layout_height=“wrap_content” android:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”> <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width=“match_parent” android:layout_height=“wrap_content” app:layout_scrollFlags=“scroll|exitUntilCollapsed” app:contentScrim=“?attr/colorPrimary”> <androidx.appcompat.widget.Toolbar android:id=“@+id/toolbar” android:layout_width=“match_parent” android:layout_height=“?attr/actionBarSize” app:layout_collapseMode=“pin” app:popupTheme=“@style/ThemeOverlay.AppCompat.Light”> <!-- Ваш заголовок, кнопки и TextView могут пойти сюда --> </androidx.appcompat.widget.Toolbar> <!-- Другие элементы интерфейса, которые должны скрываться при прокрутке вверх --> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id=“@+id/inventorRecyclerView” android:layout_width=“match_parent” android:layout_height=“match_parent” app:layout_behavior=“@string/appbar_scrolling_view_behavior” /></androidx.coordinatorlayout.widget.CoordinatorLayout>
- Assistant
Вот обновленный полный XML-макет с использованием "" вместо кавычек «» и с включением всех кнопок:
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" xmlns:tools="http://schemas.android.com/tools" tools:context=".Fragments.InventoryFragment"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:contentScrim="?attr/colorPrimary"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> <!-- Добавьте здесь свои элементы, такие как title (заголовок), кнопки и TextView --> <TextView android:id="@+id/toolbarTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Инвентарь" android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" /> </androidx.appcompat.widget.Toolbar> <!-- Поместите кнопки и другие элементы ниже Toolbar для скрытия при прокрутке --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_collapseMode="pin"> <com.google.android.material.button.MaterialButton android:id="@+id/selectAllButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="8dp" android:text="Выбрать всё" style="@style/Widget.MaterialComponents.Button.OutlinedButton"/> <com.google.android.material.button.MaterialButton android:id="@+id/sellAllButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="8dp" android:text="Продать всё" style="@style/Widget.MaterialComponents.Button.OutlinedButton"/> </LinearLayout> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/inventorRecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>Этот макет содержит
AppBarLayoutиCollapsingToolbarLayout, в котором находитсяToolbar. Каждая кнопка и TextView включены. При скролле RecyclerView содержимоеAppBarLayoutбудет скрываться.Не забудьте, что вам необходимо правильно настроить стили (
@style/Widget.MaterialComponents.Button.OutlinedButtonили любой другой стиль для кнопок, который вы хотите использовать) и в вашем коде Java/Kotlin нужно будет настроить поведениеToolbar, например, установить заголовок или обработчики нажатий.Если вы используете последние версии библиотек Material Components, возможно, вам потребуется изменить стили для кнопок или адаптировать другие части этого макета для правильного отображения в вашем приложении.