android - ViewPager : setPageTransformer 错误的视图翻译
问题描述
我正在尝试为具有 3 页的 viewpager 和自定义动画的应用程序实现介绍:setPageTransformer
居中页面中的视图不与动画一起流动 - 在从第一页到第二页的滑动结束时它返回到原始位置 - 这导致从第二页到第三页的动画错误
这是我的代码:
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))
}
}
}
有谁知道解释?
推荐阅读
- c# - 我无法绑定树视图上的 IsExpanded 属性
- python - 在多个条件下在 For 循环中过滤 pandas DataFrame 的更快方法
- react-native - 在我的博览会反应原生应用程序中添加飞溅时出错
- docker - 为什么我在 docker 网络示例中看到 172.22.0.3?
- django - Django 从 ManyToManyField 表单中获取对象的数据
- java - 为什么有些行用绿色矩形突出显示以及如何在 intellij IDEA 中将其关闭
- java - Java ObjectInpuStream 读取 DefaultTableModel
- python - 为什么我得到“str”对象不可调用错误
- methods - 无论如何,“context”和“with_context”有什么区别?
- go - 为什么 float 32 和 float64 在 go 中打印为整数?