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>>(), RecyclerView.Adapter<BaseViewHolder<T, E>>(),
KoinComponent { KoinComponent {
protected val dataSet = ArrayList<T>() protected val dataSet = ArrayList<T>() //TODO make private
val items get() = dataSet.toImmutableList() 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>() { class WebtoonAdapter(private val loader: PageLoader) : BaseRecyclerAdapter<MangaPage, Int>() {
private var lastBound = -1 var pageGravity: Int = Gravity.TOP
override fun onCreateViewHolder(parent: ViewGroup) = override fun onCreateViewHolder(parent: ViewGroup) =
WebtoonHolder(parent, loader) WebtoonHolder(parent, loader)
override fun onGetItemId(item: MangaPage) = item.id override fun onGetItemId(item: MangaPage) = item.id
override fun onBindViewHolder(holder: BaseViewHolder<MangaPage, Int>, position: Int) { override fun getExtra(item: MangaPage, position: Int) = pageGravity
super.onBindViewHolder(holder, position)
lastBound = position
}
override fun getExtra(item: MangaPage, position: Int) = if (position >= lastBound) {
Gravity.TOP
} else {
Gravity.BOTTOM
}
} }

View File

@@ -2,14 +2,18 @@ package org.koitharu.kotatsu.ui.reader.wetoon
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.Gravity
import android.view.View import android.view.View
import androidx.core.view.children import androidx.core.view.children
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import kotlin.math.sign
class WebtoonRecyclerView @JvmOverloads constructor( class WebtoonRecyclerView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr) { ) : RecyclerView(context, attrs, defStyleAttr) {
private var lastScrollDirection = 0
override fun dispatchNestedPreScroll( override fun dispatchNestedPreScroll(
dx: Int, dx: Int,
dy: Int, dy: Int,
@@ -42,7 +46,21 @@ class WebtoonRecyclerView @JvmOverloads constructor(
else -> null else -> null
} ?: return 0 } ?: return 0
var scrollY = dy var scrollY = dy
scrollY -= (child as WebtoonFrameLayout) .dispatchVerticalScroll(scrollY) scrollY -= (child as WebtoonFrameLayout).dispatchVerticalScroll(scrollY)
return dy - 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.Chip
import com.google.android.material.chip.ChipGroup import com.google.android.material.chip.ChipGroup
import org.koitharu.kotatsu.ui.common.ChipsFactory import org.koitharu.kotatsu.ui.common.ChipsFactory
import kotlin.math.roundToInt
fun View.hideKeyboard() { fun View.hideKeyboard() {
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager 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) { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy) super.onScrolled(recyclerView, dx, dy)
val item = (recyclerView.layoutManager as? LinearLayoutManager) val item = (recyclerView.layoutManager as? LinearLayoutManager)
?.findFirstVisibleItemPosition() ?.findMiddleVisibleItemPosition()
if (item != null && item != RecyclerView.NO_POSITION && item != lastItem) { if (item != null && item != RecyclerView.NO_POSITION && item != lastItem) {
lastItem = item lastItem = item
callback(item) callback(item)
@@ -219,4 +220,8 @@ fun ViewPager2.callOnPageChaneListeners() {
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e(null, "ViewPager2.callOnPageChaneListeners() failed", e) Log.e(null, "ViewPager2.callOnPageChaneListeners() failed", e)
} }
}
fun LinearLayoutManager.findMiddleVisibleItemPosition(): Int {
return ((findFirstVisibleItemPosition() + findLastVisibleItemPosition()) / 2.0).roundToInt()
} }