Android ImageSwitcher not setting initial image when used with ViewPager2.registerOnPageChangeCallback

182 Views Asked by At

ImageSwitcher not setting initial Image when used in registerOnPageChangeCallback. But if I wrap it inside Handler.postdelay(), it's working.

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>
1

There are 1 best solutions below

1
On

Instead of setting factory, we can also add view directly to the image switcher.

   /**
     * 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();
    }

We can addview to imageSwitcher. Make sure we add only 2 times only. A ViewSwitcher can only have two child views.

imageSwitcher.addView(ImageView(activity?.applicationContext))
imageSwitcher.addView(ImageView(activity?.applicationContext))

I tried adding view manually and it is working. No need to wrap around Handler.

I am also trying to find the root cause of why it is working when we wrap inside the handler with delay.