Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/extension/view/RecyclerViewExtensions.kt

46 lines
1.8 KiB
Kotlin
Raw Normal View History

2017-02-21 16:12:03 +01:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.extension.view
2020-01-26 08:35:15 +01:00
import androidx.recyclerview.widget.RecyclerView
2017-02-21 16:12:03 +01:00
import android.view.View
2020-01-26 08:35:15 +01:00
import androidx.recyclerview.widget.recyclerView
2020-06-09 00:50:51 +02:00
import kotlin.math.max
2017-02-21 16:12:03 +01:00
fun RecyclerView.LayoutManager.calculateSpaceItemHeight(child: View, spaceViewType: Int, typeStart: Int): Int {
val recyclerView = recyclerView ?: return 0
var heightBeforeSpace = 0
for (i in 0 until childCount) {
val childToMeasure = getChildAt(i)
2020-01-26 08:35:15 +01:00
val typeToMeasure = getItemViewType(childToMeasure!!)
2017-02-21 16:12:03 +01:00
if (typeToMeasure == spaceViewType) {
break
}
2017-03-02 02:24:09 +01:00
if (typeToMeasure == typeStart || heightBeforeSpace != 0 && child != childToMeasure) {
2017-02-21 16:12:03 +01:00
heightBeforeSpace += getDecoratedMeasuredHeight(childToMeasure)
}
}
if (heightBeforeSpace != 0) {
2017-06-26 08:48:05 +02:00
val spaceHeight = recyclerView.measuredHeight - recyclerView.paddingTop -
recyclerView.paddingBottom - heightBeforeSpace
2020-06-09 00:50:51 +02:00
return max(0, spaceHeight)
2017-02-21 16:12:03 +01:00
}
2017-03-02 02:24:09 +01:00
return -1
2017-02-21 16:12:03 +01:00
}