0

My ViewPager changes pages by sliding to a page, sometimes it jumps page, for example it is on 1, and it goes to page 3, sometimes it changes without stopping for example from page 3 to 2, from 2 to 3, and so on.

Listeners attached to ViewPager2

 public void attachTabLayoutToSlides(ViewPager2 slideShow) {
    if(timer==null){
        timer=new Timer();
    }
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(final TabLayout.Tab tab) {
            timer.finish();
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pageCount.setVisibility(View.VISIBLE);
                    pageCount.setText((tab.getPosition()+1)+"/"+tabLayout.getTabCount());
                }
            });
            timer.clock(3000,new Thread(){
                @Override
                public void run() {
                    super.run();
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            pageCount.setVisibility(View.GONE);
                        }
                    });
                }
            });
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
    new TabLayoutMediator(tabLayout, slideShow,
            new TabLayoutMediator.TabConfigurationStrategy() {
                @Override
                public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {

                }
            }
    ).attach();

XML of the ViewPager2:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/slideshow"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        </androidx.viewpager2.widget.ViewPager2>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs_slides"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/slideshow"
            app:tabBackground="@drawable/tab_selector"
            app:tabGravity="fill"
            app:tabIndicatorGravity="center"
            app:tabIndicatorHeight="0dp"
            app:tabMode="fixed"></com.google.android.material.tabs.TabLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/pageCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@android:drawable/screen_background_dark_transparent"
        android:text=""
        android:textSize="16sp" />
</FrameLayout>

A video of the problem: https://drive.google.com/file/d/1sx6ee0d46_Q7WmPglI4FF8VdYmD-E77r/view?usp=sharing

0 Answers0