我正在尝试使用setPageTransformer
为具有3页viewpager和自定义动画的应用实现简介。居中页面的视图不随动画一起流动-在从第一页滑到第二页的末尾,它返回原始位置-从第二页到第三页导致动画错误
这是我的代码:
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))
}
}
我正在使用“ androidx”作为片段,适配器和Viewpager:
我的适配器:
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
}
}
我缺少什么吗?
答案 0 :(得分:0)
经过数小时的查找问题。我找到了有点奇怪的解决方案 问题出在 KTX扩展
一旦我删除了它的用法,并将其更改为“ 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))
}
}
}
有人知道这个解释吗?