@@ -61,7 +61,7 @@ open class StickyNestedLayout : LinearLayout,
61
61
private var isNestedScrollingStartedByThisView = false
62
62
63
63
private lateinit var headView: View
64
- private lateinit var navView: View
64
+ private var navView: View ? = null
65
65
private lateinit var contentView: View
66
66
67
67
@Suppress(" LeakingThis" )
@@ -102,13 +102,13 @@ open class StickyNestedLayout : LinearLayout,
102
102
override fun onFinishInflate () {
103
103
super .onFinishInflate()
104
104
105
- headView = findChildView (
105
+ headView = requireChildView (
106
106
R .id.stickyHeadView, R .string.stickyHeadView, " stickyHeadView"
107
107
)
108
- navView = findChildView (
108
+ navView = optionalChildView (
109
109
R .id.stickyNavView, R .string.stickyNavView, " stickyNavView"
110
110
)
111
- contentView = findChildView (
111
+ contentView = requireChildView (
112
112
R .id.stickyContentView, R .string.stickyContentView, " stickyContentView"
113
113
)
114
114
@@ -117,24 +117,29 @@ open class StickyNestedLayout : LinearLayout,
117
117
headView.isClickable = true
118
118
}
119
119
120
- private fun findChildView (@IdRes id : Int , @StringRes strId : Int , msg : String ): View {
120
+ private fun requireChildView (@IdRes id : Int , @StringRes strId : Int , msg : String ): View {
121
+ return optionalChildView(id, strId, msg)
122
+ ? : throw StickyNestedLayoutException (
123
+ " 在StickyNestedLayout中必须要提供一个含有属性 android:id=\" @id/$msg \" 或者" +
124
+ " android:contentDescription=\" @string/$msg \" 的子View "
125
+ )
126
+ }
127
+
128
+ private fun optionalChildView (@IdRes id : Int , @StringRes strId : Int , msg : String ): View ? {
121
129
val viewOptional: View ? = findViewById(id)
122
- if (viewOptional != null ) {
123
- return viewOptional
130
+ return if (viewOptional != null ) {
131
+ viewOptional
124
132
} else {
125
133
val singleViewExpect = ArrayList <View >(1 )
126
134
findViewsWithText(singleViewExpect, string(strId), FIND_VIEWS_WITH_CONTENT_DESCRIPTION )
127
- return when {
128
- singleViewExpect.isEmpty() -> throw StickyNestedLayoutException (
129
- " 在StickyNestedLayout中必须要提供一个含有属性 android:id=\" @id/$msg \" 或者" +
130
- " android:contentDescription=\" @string/$msg \" 的子View "
131
- )
132
- singleViewExpect.size > 1 -> throw StickyNestedLayoutException (
135
+ if (singleViewExpect.size > 1 ) {
136
+ throw StickyNestedLayoutException (
133
137
" 在StickyNestedLayout中包含了多个含有属性 android:id=\" @id/$msg \" 或者" +
134
138
" android:contentDescription=\" @string/$msg \" 的子View," +
135
139
" StickyNestedLayout无法确定应该使用哪一个"
136
140
)
137
- else -> singleViewExpect.first()
141
+ } else {
142
+ singleViewExpect.firstOrNull()
138
143
}
139
144
}
140
145
}
@@ -143,7 +148,10 @@ open class StickyNestedLayout : LinearLayout,
143
148
super .onMeasure(widthMeasureSpec, heightMeasureSpec)
144
149
val wrapContent = makeMeasureSpec(0 , UNSPECIFIED )
145
150
measureChildWithMargins(headView, widthMeasureSpec, wrapContent)
146
- measureChildWithMargins(navView, widthMeasureSpec, wrapContent)
151
+ val navigationView = navView
152
+ if (navigationView != null ) {
153
+ measureChildWithMargins(navigationView, widthMeasureSpec, wrapContent)
154
+ }
147
155
val expectContentHeight = makeMeasureSpec(
148
156
measuredHeight - navViewHeight - stickyOffsetHeight,
149
157
MeasureSpec .AT_MOST
@@ -578,7 +586,7 @@ open class StickyNestedLayout : LinearLayout,
578
586
*/
579
587
@MainThread
580
588
fun scrollToNavView () {
581
- val toY = navView.y
589
+ val toY = navView?.y ? : contentView .y
582
590
scrollTo(0 , toY.toInt())
583
591
}
584
592
@@ -597,17 +605,17 @@ open class StickyNestedLayout : LinearLayout,
597
605
/* *
598
606
* 获取头部区域的高度
599
607
*/
600
- val headViewHeight get() = headView.measuredHeight
608
+ val headViewHeight: Int get() = headView.measuredHeight
601
609
602
610
/* *
603
611
* 获取导航栏条的高度
604
612
*/
605
- val navViewHeight get() = navView.measuredHeight
613
+ val navViewHeight: Int get() = navView? .measuredHeight ? : 0
606
614
607
615
/* *
608
616
* 获取下部区域的高度
609
617
*/
610
- val contentViewHeight get() = contentView.measuredHeight
618
+ val contentViewHeight: Int get() = contentView.measuredHeight
611
619
612
620
private val scrollListeners = mutableListOf<OnScrollListener >()
613
621
0 commit comments