This commit is contained in:
LagradOst 2021-10-22 20:11:26 +02:00
parent 0703b6ff36
commit 8ed1b488be
3 changed files with 50 additions and 10 deletions

View file

@ -123,12 +123,25 @@ class HomeFragment : Fragment() {
if (randomItems.isNullOrEmpty()) { if (randomItems.isNullOrEmpty()) {
toggleMainVisibility(false) toggleMainVisibility(false)
} else { } else {
val randomSize = randomItems.size
home_main_poster_recyclerview.adapter = home_main_poster_recyclerview.adapter =
HomeChildItemAdapter(randomItems, R.layout.home_result_big_grid) { callback -> HomeChildItemAdapter(randomItems, R.layout.home_result_big_grid) { callback ->
handleSearchClickCallback(activity, callback) handleSearchClickCallback(activity, callback)
} }
home_main_poster_recyclerview.post { home_main_poster_recyclerview.post {
(home_main_poster_recyclerview.layoutManager as CenterZoomLayoutManager?)?.updateSize(forceUpdate = true) (home_main_poster_recyclerview.layoutManager as CenterZoomLayoutManager?)?.let { manager ->
manager.updateSize(forceUpdate = true)
if (randomSize > 2) {
manager.scrollToPosition(randomSize / 2)
manager.snap { dx ->
home_main_poster_recyclerview?.post {
// this is the best I can do, fuck android for not including instant scroll
home_main_poster_recyclerview?.smoothScrollBy(dx,0)
}
}
}
}
} }
toggleMainVisibility(true) toggleMainVisibility(true)

View file

@ -3,7 +3,9 @@ package com.lagradost.cloudstream3.widget
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.lagradost.cloudstream3.mvvm.logError
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.min import kotlin.math.min
@ -38,17 +40,21 @@ class CenterZoomLayoutManager : LinearLayoutManager {
var largestSize = 0f var largestSize = 0f
for (i in 0 until childCount) { for (i in 0 until childCount) {
getChildAt(i)?.let { child -> getChildAt(i)?.let { child ->
val childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f try {
val d = min(d1, abs(midpoint - childMidpoint)) val childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f
val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0) val d = min(d1, abs(midpoint - childMidpoint))
child.scaleX = scale val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0)
child.scaleY = scale child.scaleX = scale
child.scaleY = scale
if (scale > largestSize) { if (scale > largestSize) {
(child.tag as Int?)?.let { tag -> (child.tag as Int?)?.let { tag ->
largestSize = scale largestSize = scale
largestTag = tag largestTag = tag
}
} }
} catch (e : Exception) {
logError(e)
} }
} }
} }
@ -72,9 +78,26 @@ class CenterZoomLayoutManager : LinearLayoutManager {
override fun onLayoutCompleted(state: RecyclerView.State?) { override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state) super.onLayoutCompleted(state)
if(waitForSnap != null) {
this.getChildAt(snapChild ?: 1)?.let { view ->
LinearSnapHelper().calculateDistanceToFinalSnap(this,view)?.get(0)?.let { dx ->
println("DX: $dx")
waitForSnap?.invoke(dx)
waitForSnap = null
}
}
}
updateSize() updateSize()
} }
private var waitForSnap : ((Int) -> Unit)? = null
private var snapChild : Int? = null
fun snap(snap : Int? = null, callback : (Int) -> Unit) {
waitForSnap = callback
snapChild = snap
}
override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int { override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int {
val orientation = orientation val orientation = orientation
return if (orientation == HORIZONTAL) { return if (orientation == HORIZONTAL) {

View file

@ -165,6 +165,10 @@
android:nextFocusUp="@id/home_change_api" android:nextFocusUp="@id/home_change_api"
android:nextFocusDown="@id/home_main_info" android:nextFocusDown="@id/home_main_info"
android:clipToPadding="false"
android:paddingStart="148dp"
android:paddingEnd="148dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:id="@+id/home_main_poster_recyclerview" android:id="@+id/home_main_poster_recyclerview"
android:orientation="horizontal" android:orientation="horizontal"