Skip to content

Commit 2c52af4

Browse files
committed
Allow navigation view missing
1 parent 849a446 commit 2c52af4

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

nestedtouch/src/main/java/mobile/yy/com/nestedtouch/StickyNestedLayout.kt

+27-19
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ open class StickyNestedLayout : LinearLayout,
6161
private var isNestedScrollingStartedByThisView = false
6262

6363
private lateinit var headView: View
64-
private lateinit var navView: View
64+
private var navView: View? = null
6565
private lateinit var contentView: View
6666

6767
@Suppress("LeakingThis")
@@ -102,13 +102,13 @@ open class StickyNestedLayout : LinearLayout,
102102
override fun onFinishInflate() {
103103
super.onFinishInflate()
104104

105-
headView = findChildView(
105+
headView = requireChildView(
106106
R.id.stickyHeadView, R.string.stickyHeadView, "stickyHeadView"
107107
)
108-
navView = findChildView(
108+
navView = optionalChildView(
109109
R.id.stickyNavView, R.string.stickyNavView, "stickyNavView"
110110
)
111-
contentView = findChildView(
111+
contentView = requireChildView(
112112
R.id.stickyContentView, R.string.stickyContentView, "stickyContentView"
113113
)
114114

@@ -117,24 +117,29 @@ open class StickyNestedLayout : LinearLayout,
117117
headView.isClickable = true
118118
}
119119

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? {
121129
val viewOptional: View? = findViewById(id)
122-
if (viewOptional != null) {
123-
return viewOptional
130+
return if (viewOptional != null) {
131+
viewOptional
124132
} else {
125133
val singleViewExpect = ArrayList<View>(1)
126134
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(
133137
"在StickyNestedLayout中包含了多个含有属性 android:id=\"@id/$msg\" 或者" +
134138
"android:contentDescription=\"@string/$msg\" 的子View," +
135139
"StickyNestedLayout无法确定应该使用哪一个"
136140
)
137-
else -> singleViewExpect.first()
141+
} else {
142+
singleViewExpect.firstOrNull()
138143
}
139144
}
140145
}
@@ -143,7 +148,10 @@ open class StickyNestedLayout : LinearLayout,
143148
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
144149
val wrapContent = makeMeasureSpec(0, UNSPECIFIED)
145150
measureChildWithMargins(headView, widthMeasureSpec, wrapContent)
146-
measureChildWithMargins(navView, widthMeasureSpec, wrapContent)
151+
val navigationView = navView
152+
if (navigationView != null) {
153+
measureChildWithMargins(navigationView, widthMeasureSpec, wrapContent)
154+
}
147155
val expectContentHeight = makeMeasureSpec(
148156
measuredHeight - navViewHeight - stickyOffsetHeight,
149157
MeasureSpec.AT_MOST
@@ -578,7 +586,7 @@ open class StickyNestedLayout : LinearLayout,
578586
*/
579587
@MainThread
580588
fun scrollToNavView() {
581-
val toY = navView.y
589+
val toY = navView?.y ?: contentView.y
582590
scrollTo(0, toY.toInt())
583591
}
584592

@@ -597,17 +605,17 @@ open class StickyNestedLayout : LinearLayout,
597605
/**
598606
* 获取头部区域的高度
599607
*/
600-
val headViewHeight get() = headView.measuredHeight
608+
val headViewHeight: Int get() = headView.measuredHeight
601609

602610
/**
603611
* 获取导航栏条的高度
604612
*/
605-
val navViewHeight get() = navView.measuredHeight
613+
val navViewHeight: Int get() = navView?.measuredHeight ?: 0
606614

607615
/**
608616
* 获取下部区域的高度
609617
*/
610-
val contentViewHeight get() = contentView.measuredHeight
618+
val contentViewHeight: Int get() = contentView.measuredHeight
611619

612620
private val scrollListeners = mutableListOf<OnScrollListener>()
613621

0 commit comments

Comments
 (0)