mirror of
				https://github.com/recloudstream/cloudstream.git
				synced 2024-08-15 01:53:11 +00:00 
			
		
		
		
	center
This commit is contained in:
		
							parent
							
								
									0703b6ff36
								
							
						
					
					
						commit
						8ed1b488be
					
				
					 3 changed files with 50 additions and 10 deletions
				
			
		|  | @ -123,12 +123,25 @@ class HomeFragment : Fragment() { | |||
|             if (randomItems.isNullOrEmpty()) { | ||||
|                 toggleMainVisibility(false) | ||||
|             } else { | ||||
|                 val randomSize = randomItems.size | ||||
|                 home_main_poster_recyclerview.adapter = | ||||
|                     HomeChildItemAdapter(randomItems, R.layout.home_result_big_grid) { callback -> | ||||
|                         handleSearchClickCallback(activity, callback) | ||||
|                     } | ||||
|                 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) | ||||
|  |  | |||
|  | @ -3,7 +3,9 @@ package com.lagradost.cloudstream3.widget | |||
| import android.content.Context | ||||
| import android.util.AttributeSet | ||||
| import androidx.recyclerview.widget.LinearLayoutManager | ||||
| import androidx.recyclerview.widget.LinearSnapHelper | ||||
| import androidx.recyclerview.widget.RecyclerView | ||||
| import com.lagradost.cloudstream3.mvvm.logError | ||||
| import kotlin.math.abs | ||||
| import kotlin.math.min | ||||
| 
 | ||||
|  | @ -38,17 +40,21 @@ class CenterZoomLayoutManager : LinearLayoutManager { | |||
|         var largestSize = 0f | ||||
|         for (i in 0 until childCount) { | ||||
|             getChildAt(i)?.let { child -> | ||||
|                 val childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f | ||||
|                 val d = min(d1, abs(midpoint - childMidpoint)) | ||||
|                 val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0) | ||||
|                 child.scaleX = scale | ||||
|                 child.scaleY = scale | ||||
|                 try { | ||||
|                     val childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f | ||||
|                     val d = min(d1, abs(midpoint - childMidpoint)) | ||||
|                     val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0) | ||||
|                     child.scaleX = scale | ||||
|                     child.scaleY = scale | ||||
| 
 | ||||
|                 if (scale > largestSize) { | ||||
|                     (child.tag as Int?)?.let { tag -> | ||||
|                         largestSize = scale | ||||
|                         largestTag = tag | ||||
|                     if (scale > largestSize) { | ||||
|                         (child.tag as Int?)?.let { tag -> | ||||
|                             largestSize = scale | ||||
|                             largestTag = tag | ||||
|                         } | ||||
|                     } | ||||
|                 } catch (e : Exception) { | ||||
|                     logError(e) | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | @ -72,9 +78,26 @@ class CenterZoomLayoutManager : LinearLayoutManager { | |||
| 
 | ||||
|     override fun onLayoutCompleted(state: RecyclerView.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() | ||||
|     } | ||||
| 
 | ||||
|     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 { | ||||
|         val orientation = orientation | ||||
|         return if (orientation == HORIZONTAL) { | ||||
|  |  | |||
|  | @ -165,6 +165,10 @@ | |||
|                         android:nextFocusUp="@id/home_change_api" | ||||
|                         android:nextFocusDown="@id/home_main_info" | ||||
| 
 | ||||
|                         android:clipToPadding="false" | ||||
|                         android:paddingStart="148dp" | ||||
|                         android:paddingEnd="148dp" | ||||
| 
 | ||||
|                         app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" | ||||
|                         android:id="@+id/home_main_poster_recyclerview" | ||||
|                         android:orientation="horizontal" | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue