From 728994a8f3d878877f470065fd874b6fc7e502d2 Mon Sep 17 00:00:00 2001 From: soraefir Date: Mon, 8 Apr 2024 23:46:58 +0200 Subject: [PATCH] Fix racecondition when button spam --- .../beans/activity/adapter/ViewPagerAdapter.kt | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/net/helcel/beans/activity/adapter/ViewPagerAdapter.kt b/app/src/main/java/net/helcel/beans/activity/adapter/ViewPagerAdapter.kt index 656eec3..01f8316 100644 --- a/app/src/main/java/net/helcel/beans/activity/adapter/ViewPagerAdapter.kt +++ b/app/src/main/java/net/helcel/beans/activity/adapter/ViewPagerAdapter.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.Lifecycle import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.widget.ViewPager2 import net.helcel.beans.activity.fragment.EditPlaceFragment +import kotlin.math.max class ViewPagerAdapter( fragmentManager: FragmentManager, @@ -17,15 +18,13 @@ class ViewPagerAdapter( private val fragmentList: MutableList = ArrayList() fun addFragment(src: EditPlaceFragment?, target: EditPlaceFragment) { - if (src != null) { - while (fragmentList.last() != src) { - fragmentList.removeLast() - notifyItemRemoved(fragmentList.size) - } + val idx = fragmentList.indexOf(src) + viewPager.currentItem = max(0, idx) + if (src != null && idx >= 0) { + fragmentList.subList(idx + 1, fragmentList.size).clear() } - println(src.toString() + " - " + target.toString()) fragmentList.add(target) - notifyItemInserted(fragmentList.size) + notifyItemRangeChanged(max(0, idx), fragmentList.size) viewPager.currentItem = fragmentList.size - 1 }