Fix webtoon page gravity

This commit is contained in:
Koitharu
2020-03-05 19:35:06 +02:00
parent cb76c54c42
commit e528cc38ae
5 changed files with 28 additions and 14 deletions

View File

@@ -10,7 +10,7 @@ abstract class BaseRecyclerAdapter<T, E>(private val onItemClickListener: OnRecy
RecyclerView.Adapter<BaseViewHolder<T, E>>(),
KoinComponent {
protected val dataSet = ArrayList<T>()
protected val dataSet = ArrayList<T>() //TODO make private
val items get() = dataSet.toImmutableList()

View File

@@ -9,21 +9,12 @@ import org.koitharu.kotatsu.ui.reader.PageLoader
class WebtoonAdapter(private val loader: PageLoader) : BaseRecyclerAdapter<MangaPage, Int>() {
private var lastBound = -1
var pageGravity: Int = Gravity.TOP
override fun onCreateViewHolder(parent: ViewGroup) =
WebtoonHolder(parent, loader)
override fun onGetItemId(item: MangaPage) = item.id
override fun onBindViewHolder(holder: BaseViewHolder<MangaPage, Int>, position: Int) {
super.onBindViewHolder(holder, position)
lastBound = position
}
override fun getExtra(item: MangaPage, position: Int) = if (position >= lastBound) {
Gravity.TOP
} else {
Gravity.BOTTOM
}
override fun getExtra(item: MangaPage, position: Int) = pageGravity
}

View File

@@ -2,14 +2,18 @@ package org.koitharu.kotatsu.ui.reader.wetoon
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import androidx.core.view.children
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.sign
class WebtoonRecyclerView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr) {
private var lastScrollDirection = 0
override fun dispatchNestedPreScroll(
dx: Int,
dy: Int,
@@ -42,7 +46,21 @@ class WebtoonRecyclerView @JvmOverloads constructor(
else -> null
} ?: return 0
var scrollY = dy
scrollY -= (child as WebtoonFrameLayout) .dispatchVerticalScroll(scrollY)
scrollY -= (child as WebtoonFrameLayout).dispatchVerticalScroll(scrollY)
return dy - scrollY
}
override fun onScrolled(dx: Int, dy: Int) {
val direction = dy.sign
if (direction != lastScrollDirection) {
(adapter as? WebtoonAdapter)?.let {
it.pageGravity = if (dy < 0) {
Gravity.BOTTOM
} else {
Gravity.TOP
}
lastScrollDirection = direction
}
}
}
}

View File

@@ -23,6 +23,7 @@ import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
import org.koitharu.kotatsu.ui.common.ChipsFactory
import kotlin.math.roundToInt
fun View.hideKeyboard() {
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
@@ -182,7 +183,7 @@ fun RecyclerView.doOnCurrentItemChanged(callback: (Int) -> Unit) {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val item = (recyclerView.layoutManager as? LinearLayoutManager)
?.findFirstVisibleItemPosition()
?.findMiddleVisibleItemPosition()
if (item != null && item != RecyclerView.NO_POSITION && item != lastItem) {
lastItem = item
callback(item)
@@ -219,4 +220,8 @@ fun ViewPager2.callOnPageChaneListeners() {
} catch (e: Throwable) {
Log.e(null, "ViewPager2.callOnPageChaneListeners() failed", e)
}
}
fun LinearLayoutManager.findMiddleVisibleItemPosition(): Int {
return ((findFirstVisibleItemPosition() + findLastVisibleItemPosition()) / 2.0).roundToInt()
}