Reputation: 1093
What I want to achieve is to have a Button
container in a CoordinatorLayout
android.support.design.widget.CoordinatorLayout
android.widget.button
that reacts to the SnackBar like the FloatingActionButton
.
That one uses a FloatingActionButton
.
Behavior which extends CoordinatorLayout.Behavior<FloatingActionButton>
.
Do I have to define a custom behaviour myself or are there any generic components that react the same way?
Upvotes: 1
Views: 1319
Reputation: 12605
You should make your own custom CoordinatorLayout.Behavior
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.snackbar.Snackbar
class SnackbarAwareBehaviour(context: Context, attrs: AttributeSet)
: CoordinatorLayout.Behavior<View>(context, attrs) {
override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
return dependency is Snackbar.SnackbarLayout
}
override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
child.translationY = Math.min(0f, dependency.translationY - dependency.height)
return true
}
override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) {
child.translationY = 0f
}
}
and you have to assign that behaviour to any direct child view of the CoordinatorLayout by using one of the below two methods
1- By using the layout_behavior
custom CoordinatorLayout
attribute
<androidx.coordinatorlayout.widget.CoordinatorLayout
...>
...
<!-- The view which has the Behaviour should be a direct child to the CoordinatorLayout -->
<Button
...
app:layout_behavior="my.package.SnackbarAwareBehaviour"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
2 - Or you can also assign the behaviour to your own custom view by implementing CoordinatorLayout.AttachedBehavior interface
class MyButton(context: Context, attrs: AttributeSet?)
: Button(context, attrs), CoordinatorLayout.AttachedBehavior {
...
override fun getBehavior(): CoordinatorLayout.Behavior<*> {
return SnackbarAwareBehaviour(context, null)
}
}
Upvotes: 2
Reputation: 363825
You can define your own layout and a custom CoordinatorLayout.Behavior
You can clone the FloatingActionButton
structure.
Something like:
@CoordinatorLayout.DefaultBehavior(ButtonLayout.Behavior.class)
public class ButtonLayout extends LinearLayout {
public static class Behavior extends
android.support.design.widget.CoordinatorLayout.Behavior<ButtonLayout> {
public boolean layoutDependsOn(CoordinatorLayout parent, ButtonLayout child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
public boolean onDependentViewChanged(CoordinatorLayout parent, ButtonLayout child, View dependency) {
if(dependency instanceof Snackbar.SnackbarLayout) {
this.updateFabTranslationForSnackbar(parent, child, dependency);
}
return false;
}
private void updateFabTranslationForSnackbar(CoordinatorLayout parent, ButtonLayout fab, View snackbar) {
//copy from FloatingActionButton.Behavior
}
}
}
Upvotes: 2