Files
mihon-bookoasis/app/src/main/java/eu/kanade/tachiyomi/widget/RecyclerViewPagerAdapter.kt
T
Ivan Iskandar da16110e1c Edge-to-edge manga details view (#5613)
* Prepare for edge-to-edge MangaController

* Fix derpy liftToScroll with our own implementation

* Edge-to-edge MangaController

Except when legacy blue theme is used.

* Save app bar lift state for controller backstack

* Fix expanded cover position after the view recycled

* Handle overlap changes when incognito mode disabled

* Tablet fixes

* Revert "Handle overlap changes when incognito mode disabled"

This reverts commit 1f492449

Breaks on rotation changes.

* Fix MangaController's swipe refresh position

* All controllers are now doing lift app bar on scroll by default

They are already doing that before so this pretty much just a cleanups.

* TachiyomiCoordinatorLayout: Support ViewPager for app bar lift state check

I'm willing to revert this if this minute detail solution is deemed too hacky xD

* Fix app bar not lifted when scrolled without fling

* Save app bar lift state across configuration changes

* Fix MangaController's swipe refresh position after configuration change

* TachiyomiCoordinatorLayout: Update ViewPager reference when controller is changed
2021-08-19 09:12:52 -04:00

61 lines
1.8 KiB
Kotlin

package eu.kanade.tachiyomi.widget
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.ViewPager
import com.nightlynexus.viewstatepageradapter.ViewStatePagerAdapter
import java.util.Stack
abstract class RecyclerViewPagerAdapter : ViewStatePagerAdapter() {
private val pool = Stack<View>()
var recycle = true
set(value) {
if (!value) pool.clear()
field = value
}
protected abstract fun createView(container: ViewGroup): View
protected abstract fun bindView(view: View, position: Int)
protected open fun recycleView(view: View, position: Int) {}
override fun createView(container: ViewGroup, position: Int): View {
val view = if (pool.isNotEmpty()) {
pool.pop().setViewPagerPositionParam(position)
} else {
createView(container)
}
bindView(view, position)
return view
}
override fun destroyView(container: ViewGroup, position: Int, view: View) {
recycleView(view, position)
if (recycle) pool.push(view)
}
/**
* Making sure that this ViewPager child view has the correct "position" layout param
* after being recycled.
*/
private fun View.setViewPagerPositionParam(position: Int): View {
val params = layoutParams
if (params is ViewPager.LayoutParams) {
if (!params.isDecor) {
try {
val positionField = ViewPager.LayoutParams::class.java.getDeclaredField("position")
positionField.isAccessible = true
positionField.setInt(params, position)
layoutParams = params
} catch (e: NoSuchFieldException) {
} catch (e: IllegalAccessException) {
}
}
}
return this
}
}