1
1
package app.layout.motion.motionlayoutexample
2
2
3
3
import android.animation.ArgbEvaluator
4
+ import android.content.res.ColorStateList
4
5
import android.graphics.Color
5
6
import android.os.Build
6
7
import android.os.Bundle
7
8
import android.os.Handler
9
+ import android.util.DisplayMetrics
10
+ import android.view.MotionEvent
8
11
import android.view.View
9
12
import android.view.WindowManager
13
+ import android.widget.LinearLayout
10
14
import android.widget.TextView
11
15
import androidx.appcompat.app.AppCompatActivity
12
16
import androidx.constraintlayout.motion.widget.MotionLayout
13
17
import androidx.core.content.ContextCompat
14
18
import androidx.recyclerview.widget.LinearLayoutManager
15
19
import 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
16
24
17
25
class DemoActivity : AppCompatActivity (), MotionLayout.TransitionListener {
18
26
@@ -26,6 +34,16 @@ class DemoActivity : AppCompatActivity(), MotionLayout.TransitionListener {
26
34
private var startColor = Color .parseColor(" #f48930" )
27
35
private var endColor = Color .parseColor(" #b55fa3" )
28
36
private var currentColor = Color .parseColor(" #b55fa3" )
37
+ private var bottomSheetBehavior: BottomSheetBehavior <LinearLayout >? = null
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
+ }
29
47
30
48
override fun onCreate (savedInstanceState : Bundle ? ) {
31
49
super .onCreate(savedInstanceState)
@@ -34,6 +52,51 @@ class DemoActivity : AppCompatActivity(), MotionLayout.TransitionListener {
34
52
setTheme()
35
53
setContentView(layout)
36
54
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
+
37
100
recyclerView!! .apply {
38
101
setHasFixedSize(true )
39
102
adapter = DummyListAdapter ()
@@ -67,11 +130,24 @@ class DemoActivity : AppCompatActivity(), MotionLayout.TransitionListener {
67
130
motionLayout = findViewById(R .id.motionLayout)
68
131
recyclerView = findViewById(R .id.recyclerView)
69
132
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
+
70
142
if (layout == R .layout.collapsing_toolbar_2) {
71
143
titleTextView = findViewById(R .id.title)
72
144
}
73
145
}
74
146
147
+ override fun onTouchEvent (event : MotionEvent ? ): Boolean {
148
+ return super .onTouchEvent(event)
149
+ }
150
+
75
151
override fun onTransitionChange (p0 : MotionLayout ? , p1 : Int , p2 : Int , progress : Float ) {
76
152
if (p0 == null )
77
153
return
0 commit comments