Я создаю приложение для Android со следующей структурой:
- Основное действие имеет нижнюю панель навигации, переключающуюся между 3 различными фрагментами
- 2 фрагментов собираютсяотображать списки элементов с плавающей кнопкой действия (FAB) для добавления новых элементов
- 3-й фрагмент покажет что-то другое, не требующее FAB.
Исходя из этого, кажется, что FAB должен «принадлежать» фрагментам списка, а не основной деятельности.
У меня сейчас проблема в том, что FAB прячется за нижней панелью навигации, как в этот вопрос .Там есть хорошее решение, но оно зависит от того, находится ли FAB в том же макете, что и нижняя панель навигации.
Есть ли способ заставить FAB не скрываться за нижней навигацией, не перемещаясьFAB до основной деятельности (которая нарушает модульность фрагментов)?
Моя схема деятельности выглядит следующим образом:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_insetEdge="bottom"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
, где заменяется «контейнер фрагментов»выделенным фрагментом, как показано ниже:
TodoListFragment fragment = new TodoListFragment();
//if we were started with an intent, pass on any special arguments
fragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
и, наконец, фрагмент имеет макет, как показано ниже:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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.support.v7.widget.RecyclerView 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/list"
android:name="..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".TodoListFragment"
tools:listitem="@layout/fragment_todolist_item" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_add_white_24dp"
android:layout_margin="16dp" />
</android.support.design.widget.CoordinatorLayout>