MainActivity: Fix snackbar overlapping FABs

This commit is contained in:
Willi Ye 2020-07-11 23:29:05 +02:00 committed by ◱ PixelyIon
parent c69e72a12e
commit 27d7839bcb
3 changed files with 46 additions and 3 deletions

View File

@ -102,7 +102,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, View.OnLongClick
} }
thread(start = true) { thread(start = true) {
val snackbar = Snackbar.make(findViewById(android.R.id.content), getString(R.string.searching_roms), Snackbar.LENGTH_INDEFINITE) val snackbar = Snackbar.make(coordinatorLayout, getString(R.string.searching_roms), Snackbar.LENGTH_INDEFINITE)
runOnUiThread { runOnUiThread {
snackbar.show() snackbar.show()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED

View File

@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: MPL-2.0
* Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
*/
package emu.skyline.views
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.snackbar.Snackbar.SnackbarLayout
/**
* Custom linear layout with support for [CoordinatorLayout] to move children, when [com.google.android.material.snackbar.Snackbar] shows up.
*/
class CustomLinearLayout : LinearLayout, CoordinatorLayout.AttachedBehavior {
constructor(context : Context) : this(context, null)
constructor(context : Context, attrs : AttributeSet?) : this(context, attrs, 0)
constructor(context : Context, attrs : AttributeSet?, defStyleAttr : Int) : super(context, attrs, defStyleAttr)
override fun getBehavior() : CoordinatorLayout.Behavior<CustomLinearLayout> = MoveUpwardBehavior()
/**
* Defines behaviour when [com.google.android.material.snackbar.Snackbar] is shown.
* Simply sets an offset to y translation to move children out of the way.
*/
class MoveUpwardBehavior : CoordinatorLayout.Behavior<CustomLinearLayout>() {
override fun layoutDependsOn(parent : CoordinatorLayout, child : CustomLinearLayout, dependency : View) : Boolean = dependency is SnackbarLayout
override fun onDependentViewChanged(parent : CoordinatorLayout, child : CustomLinearLayout, dependency : View) : Boolean {
child.translationY = (0f).coerceAtMost(dependency.translationY - dependency.height)
return true
}
override fun onDependentViewRemoved(parent : CoordinatorLayout, child : CustomLinearLayout, dependency : View) {
child.translationY = 0f
}
}
}

View File

@ -3,6 +3,7 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".MainActivity">
@ -15,7 +16,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<LinearLayout <emu.skyline.views.CustomLinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
@ -39,6 +40,6 @@
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
app:maxImageSize="26dp" app:maxImageSize="26dp"
app:srcCompat="@drawable/ic_log" /> app:srcCompat="@drawable/ic_log" />
</LinearLayout> </emu.skyline.views.CustomLinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>