ViewPager : setPageTransformer 错误的视图翻译
ViewPager : setPageTransformer wrong view translation
我正在尝试为具有 3 个页面的 viewpager 和自定义动画的应用程序实现介绍,使用:setPageTransformer
居中页面中的视图不随动画一起流动 - 在从第一页滑动到第二页的最后它 return 到原始位置 - 这导致从第二页到第三页的动画错误第
页
这是我的代码:
pager.setPageTransformer(true) { view, position ->
var mPosition = position
val pageWidth = view.width
val pageHeight = view.height
val ratio = pageWidth.toFloat() / pageHeight
if (position in 0.0..1.0) {
bubble_intro_imageView.setTranslationY(-(pageWidth * (1 - mPosition) / 3 * ratio))
notification_intro_textView.setTranslationX(pageWidth * (1 - mPosition) / 8 * ratio)
bubble_intro_textView.setTranslationY(pageWidth * (1 - mPosition) / 3 * ratio)
} else if (position in -1.0..-0.0) {
var negativePosition = -position
bubble_intro_imageView.setTranslationY(-(pageWidth * (1 - negativePosition) / 3 * ratio))
notification_intro_textView.setTranslationX((pageWidth * (1 - negativePosition) / 8 * ratio))
bubble_intro_textView.setTranslationY((pageWidth * (1 - negativePosition) / 3 * ratio))
}
}
我正在为片段、适配器和 Viewpager 使用 "androidx":
我的适配器:
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import java.util.ArrayList
class IntroPageAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(
fragmentManager,
BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
var fragments = ArrayList<Fragment>()
fun addFrag(fragment: Fragment) {
fragments.add(fragment)
}
override fun getItem(position: Int): Fragment {
return fragments[position]
}
override fun getCount(): Int {
return fragments.size
}
}
有什么我遗漏的吗?
经过几个小时的问题搜索。我找到了有点奇怪的解决方案
问题出在 KTX-extension
一旦我删除了它的用法并将其更改为 "findViewById" 它运行得非常好,动画没有中断
不懂解释
我的代码:
pager.setPageTransformer(true) { view, position ->
var mPosition = position
val pageWidth = view.width
val pageHeight = view.height
val ratio = pageWidth.toFloat() / pageHeight
notificationImageView = view.findViewById<View>(R.id.notification_intro_imageView) as ImageView?
bubbleImageView = view.findViewById<View>(R.id.bubble_intro_imageView) as ImageView?
bubbleTextView = view.findViewById<View>(R.id.bubble_intro_textView) as TextView?
if (position in 0.0..1.0) {
if (notificationImageView != null && bubbleImageView != null && bubbleTextView != null) {
notificationImageView!!.setTranslationY(-(pageWidth * (1 - mPosition) / 3 * ratio))
bubbleImageView!!.setTranslationX(pageWidth * (1 - mPosition) / 8 * ratio)
bubbleTextView!!.setTranslationY(pageWidth * (1 - mPosition) / 3 * ratio)
}
} else if (position in -1.0..-0.0) {
if (notificationImageView != null && bubbleImageView != null && bubbleTextView != null) {
var negativePosition = -position
notificationImageView!!.setTranslationY(-(pageWidth * (1 - negativePosition) / 3 * ratio))
bubbleImageView!!.setTranslationX((pageWidth * (1 - negativePosition) / 8 * ratio))
bubbleTextView!!.setTranslationY((pageWidth * (1 - negativePosition) / 3 * ratio))
}
}
}
有人知道解释吗?
我正在尝试为具有 3 个页面的 viewpager 和自定义动画的应用程序实现介绍,使用:setPageTransformer
居中页面中的视图不随动画一起流动 - 在从第一页滑动到第二页的最后它 return 到原始位置 - 这导致从第二页到第三页的动画错误第
页这是我的代码:
pager.setPageTransformer(true) { view, position ->
var mPosition = position
val pageWidth = view.width
val pageHeight = view.height
val ratio = pageWidth.toFloat() / pageHeight
if (position in 0.0..1.0) {
bubble_intro_imageView.setTranslationY(-(pageWidth * (1 - mPosition) / 3 * ratio))
notification_intro_textView.setTranslationX(pageWidth * (1 - mPosition) / 8 * ratio)
bubble_intro_textView.setTranslationY(pageWidth * (1 - mPosition) / 3 * ratio)
} else if (position in -1.0..-0.0) {
var negativePosition = -position
bubble_intro_imageView.setTranslationY(-(pageWidth * (1 - negativePosition) / 3 * ratio))
notification_intro_textView.setTranslationX((pageWidth * (1 - negativePosition) / 8 * ratio))
bubble_intro_textView.setTranslationY((pageWidth * (1 - negativePosition) / 3 * ratio))
}
}
我正在为片段、适配器和 Viewpager 使用 "androidx":
我的适配器:
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import java.util.ArrayList
class IntroPageAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(
fragmentManager,
BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
var fragments = ArrayList<Fragment>()
fun addFrag(fragment: Fragment) {
fragments.add(fragment)
}
override fun getItem(position: Int): Fragment {
return fragments[position]
}
override fun getCount(): Int {
return fragments.size
}
}
有什么我遗漏的吗?
经过几个小时的问题搜索。我找到了有点奇怪的解决方案 问题出在 KTX-extension
一旦我删除了它的用法并将其更改为 "findViewById" 它运行得非常好,动画没有中断
不懂解释
我的代码:
pager.setPageTransformer(true) { view, position ->
var mPosition = position
val pageWidth = view.width
val pageHeight = view.height
val ratio = pageWidth.toFloat() / pageHeight
notificationImageView = view.findViewById<View>(R.id.notification_intro_imageView) as ImageView?
bubbleImageView = view.findViewById<View>(R.id.bubble_intro_imageView) as ImageView?
bubbleTextView = view.findViewById<View>(R.id.bubble_intro_textView) as TextView?
if (position in 0.0..1.0) {
if (notificationImageView != null && bubbleImageView != null && bubbleTextView != null) {
notificationImageView!!.setTranslationY(-(pageWidth * (1 - mPosition) / 3 * ratio))
bubbleImageView!!.setTranslationX(pageWidth * (1 - mPosition) / 8 * ratio)
bubbleTextView!!.setTranslationY(pageWidth * (1 - mPosition) / 3 * ratio)
}
} else if (position in -1.0..-0.0) {
if (notificationImageView != null && bubbleImageView != null && bubbleTextView != null) {
var negativePosition = -position
notificationImageView!!.setTranslationY(-(pageWidth * (1 - negativePosition) / 3 * ratio))
bubbleImageView!!.setTranslationX((pageWidth * (1 - negativePosition) / 8 * ratio))
bubbleTextView!!.setTranslationY((pageWidth * (1 - negativePosition) / 3 * ratio))
}
}
}
有人知道解释吗?