mirror of
https://github.com/YTVanced/VancedManager
synced 2025-01-05 15:01:01 +00:00
fixed viewpager navigation on RTL layout
This commit is contained in:
parent
6c506c989c
commit
85e2d0c5c2
1 changed files with 22 additions and 20 deletions
|
@ -1,10 +1,10 @@
|
||||||
package com.vanced.manager.ui
|
package com.vanced.manager.ui
|
||||||
|
|
||||||
import android.animation.Animator
|
|
||||||
import android.animation.ValueAnimator
|
import android.animation.ValueAnimator
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.animation.AccelerateDecelerateInterpolator
|
import android.util.LayoutDirection
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.animation.addListener
|
||||||
import androidx.viewpager2.widget.ViewPager2
|
import androidx.viewpager2.widget.ViewPager2
|
||||||
import com.vanced.manager.adapter.WelcomePageAdapter
|
import com.vanced.manager.adapter.WelcomePageAdapter
|
||||||
import com.vanced.manager.databinding.ActivityWelcomeBinding
|
import com.vanced.manager.databinding.ActivityWelcomeBinding
|
||||||
|
@ -12,21 +12,22 @@ import kotlin.math.abs
|
||||||
|
|
||||||
class WelcomeActivity : AppCompatActivity() {
|
class WelcomeActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private lateinit var viewPager2: ViewPager2
|
|
||||||
private lateinit var binding: ActivityWelcomeBinding
|
private lateinit var binding: ActivityWelcomeBinding
|
||||||
|
private var isRtl = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityWelcomeBinding.inflate(layoutInflater)
|
binding = ActivityWelcomeBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
viewPager2 = binding.welcomeViewpager
|
isRtl = resources.configuration.layoutDirection == LayoutDirection.RTL
|
||||||
viewPager2.apply {
|
|
||||||
|
binding.welcomeViewpager.apply {
|
||||||
adapter = WelcomePageAdapter(this@WelcomeActivity)
|
adapter = WelcomePageAdapter(this@WelcomeActivity)
|
||||||
isUserInputEnabled = false
|
isUserInputEnabled = false
|
||||||
setPageTransformer { page, position ->
|
setPageTransformer { page, position ->
|
||||||
page.apply {
|
page.apply {
|
||||||
val pageWidth = width
|
val pageWidth = width.toFloat()
|
||||||
//Thank you, fragula dev!
|
//Thank you, fragula dev!
|
||||||
when {
|
when {
|
||||||
position > 0 && position < 1 -> {
|
position > 0 && position < 1 -> {
|
||||||
|
@ -35,7 +36,7 @@ class WelcomeActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
position > -1 && position <= 0 -> {
|
position > -1 && position <= 0 -> {
|
||||||
alpha = 1.0f - abs(position * 0.7f)
|
alpha = 1.0f - abs(position * 0.7f)
|
||||||
translationX = -pageWidth * position / 1.3F
|
translationX = pageWidth.rtlCompat * position / 1.3F
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,17 +45,21 @@ class WelcomeActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBackPressed() {
|
override fun onBackPressed() {
|
||||||
if (viewPager2.currentItem == 0) {
|
with (binding) {
|
||||||
super.onBackPressed()
|
if (welcomeViewpager.currentItem == 0) {
|
||||||
} else {
|
super.onBackPressed()
|
||||||
navigateTo(viewPager2.currentItem - 1)
|
} else {
|
||||||
|
navigateTo(welcomeViewpager.currentItem - 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun navigateTo(position: Int) {
|
fun navigateTo(position: Int) {
|
||||||
viewPager2.currentPosition = position
|
binding.welcomeViewpager.currentPosition = position
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val Float.rtlCompat get() = if (isRtl) this else -this
|
||||||
|
|
||||||
//Shit way to implement animation duration, but at least it works
|
//Shit way to implement animation duration, but at least it works
|
||||||
private var ViewPager2.currentPosition: Int
|
private var ViewPager2.currentPosition: Int
|
||||||
get() = currentItem
|
get() = currentItem
|
||||||
|
@ -66,16 +71,13 @@ class WelcomeActivity : AppCompatActivity() {
|
||||||
addUpdateListener { valueAnimator ->
|
addUpdateListener { valueAnimator ->
|
||||||
val currentValue = valueAnimator.animatedValue as Int
|
val currentValue = valueAnimator.animatedValue as Int
|
||||||
val currentPxToDrag = (currentValue - previousValue).toFloat()
|
val currentPxToDrag = (currentValue - previousValue).toFloat()
|
||||||
fakeDragBy(-currentPxToDrag)
|
fakeDragBy(currentPxToDrag.rtlCompat)
|
||||||
previousValue = currentValue
|
previousValue = currentValue
|
||||||
}
|
}
|
||||||
addListener(object : Animator.AnimatorListener {
|
addListener(
|
||||||
override fun onAnimationStart(animation: Animator?) { beginFakeDrag() }
|
onStart = { beginFakeDrag() },
|
||||||
override fun onAnimationEnd(animation: Animator?) { endFakeDrag() }
|
onEnd = { endFakeDrag() }
|
||||||
override fun onAnimationCancel(animation: Animator?) {}
|
)
|
||||||
override fun onAnimationRepeat(animation: Animator?) {}
|
|
||||||
})
|
|
||||||
interpolator = AccelerateDecelerateInterpolator()
|
|
||||||
duration = 500
|
duration = 500
|
||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue