From 48e6ed28d17f0bc38550bf4e59c61e3d2a91ca07 Mon Sep 17 00:00:00 2001 From: HissPirat Date: Tue, 14 Jul 2020 11:34:43 +0200 Subject: [PATCH] #2186 scrolling finally works for reader with two bars --- ...rollingViewWithBottomNavigationBehavior.kt | 64 +++++++++++++++++++ .../res/layout/activity_new_navigation.xml | 42 ++++++------ 2 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/nav/helper/ScrollingViewWithBottomNavigationBehavior.kt diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/helper/ScrollingViewWithBottomNavigationBehavior.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/helper/ScrollingViewWithBottomNavigationBehavior.kt new file mode 100644 index 000000000..f42d1ec02 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/helper/ScrollingViewWithBottomNavigationBehavior.kt @@ -0,0 +1,64 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.nav.helper + +import android.content.Context +import android.util.AttributeSet +import android.view.View +import android.view.ViewGroup +import androidx.coordinatorlayout.widget.CoordinatorLayout +import com.google.android.material.appbar.AppBarLayout +import com.google.android.material.bottomappbar.BottomAppBar +import com.google.android.material.bottomnavigation.BottomNavigationView +import org.kiwix.kiwixmobile.R + +class ScrollingViewWithBottomNavigationBehavior(context: Context, attrs: AttributeSet) : + AppBarLayout.ScrollingViewBehavior(context, attrs) { + // We add a bottom margin to avoid the bottom navigation bar + private var bottomMargin = 0 + + override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean = + super.layoutDependsOn(parent, child, dependency) || dependency is BottomNavigationView + + override fun onDependentViewChanged( + parent: CoordinatorLayout, + child: View, + dependency: View + ): Boolean { + val result = super.onDependentViewChanged(parent, child, dependency) + val readerBottomAppBar: BottomAppBar? = child.findViewById(R.id.bottom_toolbar) + if (readerBottomAppBar != null) { + val navBarYPosition = dependency.y + val coordinatorHeight = parent.height + val readerNavNewMargin = (coordinatorHeight - navBarYPosition).toInt() + if (dependency is BottomNavigationView && readerNavNewMargin != bottomMargin) { + bottomMargin = readerNavNewMargin + val layout = readerBottomAppBar.layoutParams as ViewGroup.MarginLayoutParams + layout.bottomMargin = bottomMargin + readerBottomAppBar.requestLayout() + return true + } + } + return result + } + + override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) { + super.onDependentViewRemoved(parent, child, dependency) + } +} diff --git a/app/src/main/res/layout/activity_new_navigation.xml b/app/src/main/res/layout/activity_new_navigation.xml index 969c31672..13fd44600 100644 --- a/app/src/main/res/layout/activity_new_navigation.xml +++ b/app/src/main/res/layout/activity_new_navigation.xml @@ -22,28 +22,32 @@ android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" - android:orientation="vertical" - android:weightSum="1"> + android:orientation="vertical"> - + android:layout_height="match_parent"> - + + + +