From 69869115f6531f62013e6c4c3bdfb750bc26c57f Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Tue, 25 May 2021 03:08:43 +0700 Subject: [PATCH] TabbedBottomSheetDialog: Fix scrollable pages (#5173) --- .../widget/sheet/BottomSheetViewPager.kt | 54 +++++++++++++++++++ .../main/res/layout/common_tabbed_sheet.xml | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/sheet/BottomSheetViewPager.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/BottomSheetViewPager.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/BottomSheetViewPager.kt new file mode 100644 index 0000000000..af8be9aaf7 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/BottomSheetViewPager.kt @@ -0,0 +1,54 @@ +package eu.kanade.tachiyomi.widget.sheet +import android.content.Context +import android.util.AttributeSet +import android.view.View +import androidx.viewpager.widget.ViewPager +import java.lang.reflect.Field + +/** + * From https://github.com/kafumi/android-bottomsheet-viewpager + */ +class BottomSheetViewPager @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null +) : ViewPager(context, attrs) { + + private val positionField: Field = LayoutParams::class.java.getDeclaredField("position").also { + it.isAccessible = true + } + + override fun getChildAt(index: Int): View { + val currentView = getCurrentView() ?: return super.getChildAt(index) + return if (index == 0) { + currentView + } else { + var view = super.getChildAt(index) + if (view == currentView) { + view = super.getChildAt(0) + } + return view + } + } + + private fun getCurrentView(): View? { + for (i in 0 until childCount) { + val child = super.getChildAt(i) + val lp = child.layoutParams as? LayoutParams + if (lp != null) { + val position = positionField.getInt(lp) + if (!lp.isDecor && currentItem == position) { + return child + } + } + } + return null + } + + init { + addOnPageChangeListener(object : SimpleOnPageChangeListener() { + override fun onPageSelected(position: Int) { + requestLayout() + } + }) + } +} diff --git a/app/src/main/res/layout/common_tabbed_sheet.xml b/app/src/main/res/layout/common_tabbed_sheet.xml index 39a8044a93..f4032422ba 100644 --- a/app/src/main/res/layout/common_tabbed_sheet.xml +++ b/app/src/main/res/layout/common_tabbed_sheet.xml @@ -44,7 +44,7 @@ -