Created
December 5, 2022 14:53
-
-
Save NurseyitTursunkulov/94718dc86663355e4c530593d2ba1e35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.check24.techtalk.recyclerview.bigtosmall | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.LayoutInflater | |
import android.view.View | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import androidx.recyclerview.widget.RecyclerView | |
import de.check24.techtalk.common.BaseActivity | |
import de.check24.techtalk.databinding.ActivityRecyclerviewBigToSmallBinding | |
import de.check24.techtalk.recyclerview.bigtosmall.adapter.RecyclerViewBigToSmallAdapter | |
/** | |
* Created by kevin.toerpsch on 01.03.2021 | |
* RecyclerAdapterActivity | |
*/ | |
class RecyclerBigToSmallActivity : BaseActivity<ActivityRecyclerviewBigToSmallBinding>() { | |
override val bindingInflater: (LayoutInflater) -> ActivityRecyclerviewBigToSmallBinding | |
get() = ActivityRecyclerviewBigToSmallBinding::inflate | |
private var actualScrollTo: Int = 0 | |
private val bigRecyclerViewAdapter by lazy { RecyclerViewBigToSmallAdapter(true) } | |
private val smallRecyclerViewAdapter by lazy { RecyclerViewBigToSmallAdapter(false) } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding?.apply { | |
recyclerViewBig.apply { | |
layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false) | |
adapter = bigRecyclerViewAdapter | |
setOnScrollChangeListener { _, _, _, _, _ -> | |
val firstView = findFirstVisibleView(recyclerViewBig.layoutManager as LinearLayoutManager) | |
val position = firstView?.let { recyclerViewBig.getChildAdapterPosition(it) } | |
val scrollTo = (recyclerViewBig.computeHorizontalScrollOffset() * getRecyclerViewScrollSize(recyclerViewSmall)) / getRecyclerViewScrollSize(recyclerViewBig) | |
firstView?.let { | |
val offset: Float = (-firstView.x) / firstView.measuredWidth | |
Log.d("Nurs","ofset = ${recyclerViewBig.computeHorizontalScrollOffset()} postipon = ${position}" + | |
" smalpos = ${offset} diff = ${scrollTo - actualScrollTo}") | |
if (position != null) { | |
recyclerViewSmall.getChildAt(position-3)?.apply { | |
scaleY = ((1-offset)/2) | |
} | |
recyclerViewSmall.getChildAt(position -2)?.apply { | |
scaleY = 1-offset | |
} | |
recyclerViewSmall.getChildAt(position -1)?.apply { | |
scaleY = 1f | |
} | |
recyclerViewSmall.getChildAt(position)?.apply { | |
scaleY = 1f | |
} | |
recyclerViewSmall.getChildAt(position+1)?.apply { | |
scaleY = 1f | |
} | |
recyclerViewSmall.getChildAt(position+2)?.apply { | |
scaleY = offset | |
} | |
recyclerViewSmall.getChildAt(position+3)?.apply { | |
scaleY = (offset/2) | |
} | |
} | |
scrollSmallToPos(scrollTo,offset) | |
} | |
if (scrollTo>actualScrollTo){ | |
}else{ | |
} | |
} | |
} | |
recyclerViewSmall.apply { | |
layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false) | |
adapter = smallRecyclerViewAdapter | |
} | |
} | |
} | |
private fun findFirstVisibleView(layoutManager: LinearLayoutManager): View? { | |
val childCount = layoutManager.childCount | |
if (childCount == 0) { | |
return null | |
} | |
var closestChild: View? = null | |
var firstVisibleChild = Int.MAX_VALUE | |
for (i in 0 until childCount) { | |
val child = layoutManager.getChildAt(i) | |
val childStart = child!!.x.toInt() | |
if (childStart + child.measuredWidth < firstVisibleChild) { | |
firstVisibleChild = childStart | |
closestChild = child | |
} | |
} | |
return closestChild | |
} | |
private fun scrollSmallToPos(scrollTo: Int,offset:Float) { | |
binding?.recyclerViewSmall?.scrollBy(scrollTo - actualScrollTo, 0) | |
[email protected] = scrollTo | |
} | |
private fun getRecyclerViewScrollSize(recyclerView: RecyclerView): Int = recyclerView.computeHorizontalScrollRange() - recyclerView.computeHorizontalScrollExtent() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment