ImageSwitcher在registerOnPageChangeCallback中使用时不设置初始图像。但是如果我把它放在Handler.postdelay()
里面,它就能工作了
IntroFragment.kg
class IntroFragment : BaseFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.fragment_intro, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val viewPagerAdapter = IntroViewPagerAdapter(childFragmentManager, lifecycle)
viewPager2.adapter = viewPagerAdapter
val images = arrayOf(R.drawable.ic_knowledge, R.drawable.ic_privacy)
viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
private var skipButtonVisible = true
override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageSelected(position: Int) {
imageSwitcher.setImageResource(images[position])
}
})
imageSwitcher.setFactory { ImageView(activity?.applicationContext) }
imageSwitcher.setImageResource(images[0])
}
}
IntroViewPagerAdapter.kt
class IntroViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : FragmentStateAdapter(fragmentManager, lifecycle) {
private val fragments = listOf<Fragment>(
SlideFragment.newInstance(titleText, R.string.Description1),
SlideFragment.newInstance(titleText2, R.string.Description2),
)
override fun getItemCount(): Int = fragments.size
override fun createFragment(position: Int): Fragment = fragments[position]
}
fragment_intro.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/introLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:windowBackground"
android:clickable="true">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="280dp"
android:layout_height="280dp"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"
android:transitionName="welcome_wallet_logo"
app:layout_constraintBottom_toTopOf="@+id/verticalCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
除了设置factory,我们还可以直接将view添加到图像切换器中。
/**
* Sets the factory used to create the two views between which the
* ViewSwitcher will flip. Instead of using a factory, you can call
* {@link #addView(android.view.View, int, android.view.ViewGroup.LayoutParams)}
* twice.
*
* @param factory the view factory used to generate the switcher's content
*/
public void setFactory(ViewFactory factory) {
mFactory = factory;
obtainView();
obtainView();
}
我们可以给imageSwitcher添加视图。确保我们只加了2次。一个ViewSwitcher只能有两个子视图。
imageSwitcher.addView(ImageView(activity?.applicationContext))
imageSwitcher.addView(ImageView(activity?.applicationContext))
我尝试手动添加视图,它正在工作。不需要绕行Handler。
我也试图找到为什么它是工作的根本原因,当我们包装内部的处理程序与延迟。