mihon/app/src/main/java/eu/kanade/tachiyomi/widget/RecyclerViewPagerAdapter.kt

37 lines
981 B
Kotlin
Raw Normal View History

2016-08-22 12:54:16 +02:00
package eu.kanade.tachiyomi.widget
import android.view.View
import android.view.ViewGroup
2017-05-06 15:49:39 +02:00
import com.nightlynexus.viewstatepageradapter.ViewStatePagerAdapter
2020-01-29 04:47:57 +01:00
import java.util.Stack
2016-08-22 12:54:16 +02:00
2017-05-06 15:49:39 +02:00
abstract class RecyclerViewPagerAdapter : ViewStatePagerAdapter() {
2016-08-22 12:54:16 +02:00
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) {}
2017-05-06 15:49:39 +02:00
override fun createView(container: ViewGroup, position: Int): View {
2016-08-22 12:54:16 +02:00
val view = if (pool.isNotEmpty()) pool.pop() else createView(container)
bindView(view, position)
return view
}
2017-05-06 15:49:39 +02:00
override fun destroyView(container: ViewGroup, position: Int, view: View) {
2016-08-22 12:54:16 +02:00
recycleView(view, position)
if (recycle) pool.push(view)
}
2020-01-29 04:47:57 +01:00
}