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