summaryrefslogtreecommitdiff
path: root/app/src/main/java/foundation/e/advancedprivacy/common/extensions/ViewPager2Extensions.kt
blob: e17d69245e3f0905343956f5aa49609fcf45fb2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * Copyright (C) 2023 MURENA SAS
 *
 * 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 <https://www.gnu.org/licenses/>.
 */
package foundation.e.advancedprivacy.common.extensions

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2

fun ViewPager2.findViewHolderForAdapterPosition(position: Int): RecyclerView.ViewHolder? {
    return (getChildAt(0) as RecyclerView).findViewHolderForAdapterPosition(position)
}

fun ViewPager2.updatePagerHeightForChild(itemView: View) {
    itemView.post {
        val wMeasureSpec =
            View.MeasureSpec.makeMeasureSpec(itemView.width, View.MeasureSpec.EXACTLY)
        val hMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        itemView.measure(wMeasureSpec, hMeasureSpec)

        if (layoutParams.height != itemView.measuredHeight) {
            layoutParams = (layoutParams)
                .also { lp ->
                    // applying Fragment Root View Height to
                    // the pager LayoutParams, so they match
                    lp.height = itemView.measuredHeight
                }
        }
    }
}