Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active August 16, 2020 11:21
Show Gist options
  • Save Skyyo/2a2bf5ce612fd4415446cec8b466fdc9 to your computer and use it in GitHub Desktop.
Save Skyyo/2a2bf5ce612fd4415446cec8b466fdc9 to your computer and use it in GitHub Desktop.
rv as bottom sheet #bottom_sheet
val behavior = from(bottomSheetRv)
behavior.addBottomSheetCallback(object : BottomSheetCallback() {
override fun onStateChanged(
@NonNull bottomSheet: View,
newState: Int
) {
if (newState == STATE_EXPANDED) {
// In the EXPANDED STATE apply a new MaterialShapeDrawable with rounded cornes
val newMaterialShapeDrawable: MaterialShapeDrawable = createMaterialShapeDrawable(bottomSheetRv)
ViewCompat.setBackground(
bottomSheet,
newMaterialShapeDrawable
)
}
}
override fun onSlide(
@NonNull bottomSheet: View,
slideOffset: Float
) {
}
})
// behavior.state = STATE_COLLAPSED / STATE_EXPANDED
private fun createMaterialShapeDrawable(bottomSheet: View): MaterialShapeDrawable {
val currentMaterialShapeDrawable =
bottomSheet.background as MaterialShapeDrawable
val shapeAppearanceModel = ShapeAppearanceModel()
.toBuilder()
.setAllCorners(
CornerFamily.ROUNDED,
dpToPx(16f)
)
.build()
val newMaterialShapeDrawable =
MaterialShapeDrawable(shapeAppearanceModel)
// Copy the attributes in the new MaterialShapeDrawable
newMaterialShapeDrawable.initializeElevationOverlay(context)
newMaterialShapeDrawable.fillColor = currentMaterialShapeDrawable.fillColor
newMaterialShapeDrawable.tintList = currentMaterialShapeDrawable.tintList
newMaterialShapeDrawable.elevation = currentMaterialShapeDrawable.elevation
newMaterialShapeDrawable.strokeWidth = currentMaterialShapeDrawable.strokeWidth
newMaterialShapeDrawable.strokeColor = currentMaterialShapeDrawable.strokeColor
return newMaterialShapeDrawable
}
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/bottomSheetRv"
style="@style/CustomBottomSheetDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />
<style name="CustomBottomSheetDialog" parent="@style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="behavior_peekHeight">96dp</item>
<item name="behavior_hideable">false</item>
<item name="android:elevation">8dp</item>
<item name="shapeAppearance">@style/ShapeTopCornered</item>
</style>
<style name="ShapeTopCornered">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">16dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment