11package app.layout.motion.motionlayoutexample
22
33import android.animation.ArgbEvaluator
4+ import android.content.res.ColorStateList
45import android.graphics.Color
56import android.os.Build
67import android.os.Bundle
78import android.os.Handler
9+ import android.util.DisplayMetrics
10+ import android.view.MotionEvent
811import android.view.View
912import android.view.WindowManager
13+ import android.widget.LinearLayout
1014import android.widget.TextView
1115import androidx.appcompat.app.AppCompatActivity
1216import androidx.constraintlayout.motion.widget.MotionLayout
1317import androidx.core.content.ContextCompat
1418import androidx.recyclerview.widget.LinearLayoutManager
1519import androidx.recyclerview.widget.RecyclerView
20+ import com.google.android.material.bottomsheet.BottomSheetBehavior
21+ import com.google.android.material.shape.CornerFamily
22+ import com.google.android.material.shape.MaterialShapeDrawable
23+ import com.google.android.material.shape.ShapeAppearanceModel
1624
1725class DemoActivity : AppCompatActivity (), MotionLayout.TransitionListener {
1826
@@ -26,6 +34,16 @@ class DemoActivity : AppCompatActivity(), MotionLayout.TransitionListener {
2634 private var startColor = Color .parseColor(" #f48930" )
2735 private var endColor = Color .parseColor(" #b55fa3" )
2836 private var currentColor = Color .parseColor(" #b55fa3" )
37+ private lateinit var bottomSheetBehavior: BottomSheetBehavior <LinearLayout >
38+
39+ fun getScreenHeight (windowManager : WindowManager ? ): Int {
40+ if (windowManager == null ) {
41+ return 0
42+ }
43+ val metrics = DisplayMetrics ()
44+ windowManager.defaultDisplay.getMetrics(metrics)
45+ return metrics.heightPixels
46+ }
2947
3048 override fun onCreate (savedInstanceState : Bundle ? ) {
3149 super .onCreate(savedInstanceState)
@@ -34,6 +52,51 @@ class DemoActivity : AppCompatActivity(), MotionLayout.TransitionListener {
3452 setTheme()
3553 setContentView(layout)
3654 initViews()
55+
56+ // Initialize the bottom sheet behavior
57+ val bottomSheet: LinearLayout ? = findViewById(R .id.bottom_sheet)
58+ if (null != bottomSheet) {
59+ val layoutParams = bottomSheet?.layoutParams
60+ layoutParams?.height = getScreenHeight(windowManager)
61+ bottomSheet?.layoutParams = layoutParams
62+
63+ bottomSheetBehavior = BottomSheetBehavior .from(bottomSheet)
64+ // bottomSheetBehavior.isDraggable = false
65+ // Set the initial state of the bottom sheet
66+ bottomSheetBehavior.state = BottomSheetBehavior .STATE_COLLAPSED
67+
68+ // Set the peek height (the height of the bottom sheet when it is collapsed)
69+ bottomSheetBehavior.peekHeight = 100
70+ bottomSheetBehavior.isFitToContents = false
71+ // Set the half expanded ratio (the ratio of the height of the bottom sheet when it is half expanded)
72+ bottomSheetBehavior.halfExpandedRatio = 0.6f
73+
74+ val shapeAppearanceModel = ShapeAppearanceModel .Builder ()
75+ .setTopLeftCorner(CornerFamily .ROUNDED , 36f )
76+ .setTopRightCorner(CornerFamily .ROUNDED , 36f )
77+ .build()
78+
79+ val materialShapeDrawable = MaterialShapeDrawable (shapeAppearanceModel).apply {
80+ fillColor = ColorStateList .valueOf(Color .WHITE ) // Set the desired color
81+ }
82+ bottomSheet.background = materialShapeDrawable
83+
84+ // Set the bottom sheet callback to handle changes in state
85+ bottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior .BottomSheetCallback () {
86+ override fun onStateChanged (bottomSheet : View , newState : Int ) {
87+ // Handle state change
88+
89+ if (newState == BottomSheetBehavior .STATE_EXPANDED ) {
90+ // bottomSheetBehavior.isDraggable=false
91+ }
92+ }
93+
94+ override fun onSlide (bottomSheet : View , slideOffset : Float ) {
95+ // Handle slide
96+ }
97+ })
98+ }
99+
37100 recyclerView!! .apply {
38101 setHasFixedSize(true )
39102 adapter = DummyListAdapter ()
@@ -67,11 +130,24 @@ class DemoActivity : AppCompatActivity(), MotionLayout.TransitionListener {
67130 motionLayout = findViewById(R .id.motionLayout)
68131 recyclerView = findViewById(R .id.recyclerView)
69132
133+ // Intercept the recyclerView onTouch event
134+ recyclerView?.setOnTouchListener { _, event ->
135+ if (bottomSheetBehavior.state == BottomSheetBehavior .STATE_HALF_EXPANDED ) {
136+ // Disable the recyclerView scroll event when the BottomSheet is in Half Expand
137+ return @setOnTouchListener true
138+ }
139+ return @setOnTouchListener false
140+ }
141+
70142 if (layout == R .layout.collapsing_toolbar_2) {
71143 titleTextView = findViewById(R .id.title)
72144 }
73145 }
74146
147+ override fun onTouchEvent (event : MotionEvent ? ): Boolean {
148+ return super .onTouchEvent(event)
149+ }
150+
75151 override fun onTransitionChange (p0 : MotionLayout ? , p1 : Int , p2 : Int , progress : Float ) {
76152 if (p0 == null )
77153 return
0 commit comments