#2186 scrolling finally works for reader with two bars

This commit is contained in:
HissPirat 2020-07-14 11:34:43 +02:00
parent 0c400205ab
commit 48e6ed28d1
2 changed files with 87 additions and 19 deletions

View File

@ -0,0 +1,64 @@
/*
* Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
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)
}
}

View File

@ -22,28 +22,32 @@
android:id="@+id/container" android:id="@+id/container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical">
android:weightSum="1">
<fragment <org.kiwix.kiwixmobile.core.utils.NestedCoordinatorLayout
android:id="@+id/nav_host_fragment"
android:name="org.kiwix.kiwixmobile.main.NavigationHostFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="match_parent">
android:layout_gravity="bottom"
android:layout_weight="1"
app:defaultNavHost="true"
app:layout_dodgeInsetEdges="bottom"
app:navGraph="@navigation/kiwix_nav_graph"
tools:ignore="FragmentTagUsage" />
<com.google.android.material.bottomnavigation.BottomNavigationView <fragment
android:id="@+id/nav_view" android:id="@+id/nav_host_fragment"
android:layout_width="fill_parent" android:name="org.kiwix.kiwixmobile.main.NavigationHostFragment"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_gravity="bottom" android:layout_height="match_parent"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" android:layout_gravity="bottom"
app:menu="@menu/menu_bottom_nav" /> app:defaultNavHost="true"
app:layout_behavior=".nav.helper.ScrollingViewWithBottomNavigationBehavior"
app:layout_dodgeInsetEdges="bottom"
app:navGraph="@navigation/kiwix_nav_graph"
tools:ignore="FragmentTagUsage" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
app:menu="@menu/menu_bottom_nav" />
</org.kiwix.kiwixmobile.core.utils.NestedCoordinatorLayout>
</LinearLayout> </LinearLayout>