tommybuonomo / dotsindicator

Three material Dots Indicators for view pagers in Android !
Apache License 2.0
3.46k stars 354 forks source link

IllegalStateException when click on a dot while fakeDragging #152

Open Mohammad-Es0281 opened 2 years ago

Mohammad-Es0281 commented 2 years ago

Duo to the setCurrentItem() in ViewPager2, Changing currentItem is not allowed while fakeDragging

public void setCurrentItem(int item, boolean smoothScroll) {
    if (isFakeDragging()) {
        throw new IllegalStateException("Cannot change current item when ViewPager2 is fake "
            + "dragging");
    }
    setCurrentItemInternal(item, smoothScroll);
}

So it's good to check fakeDragging state in the blow code(and also in other dotsIndicator types) before changing currentItem https://github.com/tommybuonomo/dotsindicator/blob/82d9884eda9634e6d9c89dccca05c6edc9afcb2c/viewpagerdotsindicator/src/main/kotlin/com/tbuonomo/viewpagerdotsindicator/SpringDotsIndicator.kt#L111-L115

And change code as Below

dot.setOnClickListener { 
    if (dotsClickable && index < pager?.count ?: 0 && !pager.isFakeDragging) { 
        pager!!.setCurrentItem(index, true) 
    } 
}