ViewPager: Change how initial page is selected

Change-Id: Id1a7f29526dc6957dfed61ba016c89e9b45638a2
This commit is contained in:
SpiritCroc 2022-04-08 10:08:36 +02:00
parent 66f5b5c771
commit 68c9c5459d
1 changed files with 20 additions and 9 deletions

View File

@ -22,6 +22,7 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.Toast import android.widget.Toast
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -546,7 +547,7 @@ class HomeDetailFragment @Inject constructor(
val unsafeSpaces = spaces?.map { it.roomId } ?: listOf() val unsafeSpaces = spaces?.map { it.roomId } ?: listOf()
val selectedSpaceId = (roomGroupingMethod as? RoomGroupingMethod.BySpace)?.spaceSummary?.roomId val selectedSpaceId = (roomGroupingMethod as? RoomGroupingMethod.BySpace)?.spaceSummary?.roomId
val selectedIndex = getPageIndexForSpaceId(selectedSpaceId, unsafeSpaces) val selectedIndex = getPageIndexForSpaceId(selectedSpaceId, unsafeSpaces)
val pagingEnabled = pagingAllowed && roomGroupingMethod is RoomGroupingMethod.BySpace && unsafeSpaces.size > 0 && selectedIndex != null val pagingEnabled = pagingAllowed && roomGroupingMethod is RoomGroupingMethod.BySpace && unsafeSpaces.isNotEmpty() && selectedIndex != null
val safeSpaces = if (pagingEnabled) unsafeSpaces else listOf() val safeSpaces = if (pagingEnabled) unsafeSpaces else listOf()
// Check if we need to recreate the adapter for a new tab // Check if we need to recreate the adapter for a new tab
if (oldAdapter != null) { if (oldAdapter != null) {
@ -625,7 +626,15 @@ class HomeDetailFragment @Inject constructor(
views.roomListContainerPager.adapter = adapter views.roomListContainerPager.adapter = adapter
if (pagingEnabled) { if (pagingEnabled) {
views.roomListContainerPager.post { // May be better than viewPager.post()? https://stackoverflow.com/a/57516428
val pagerRecyclerView = views.roomListContainerPager.getChildAt(0)
pagerRecyclerView.apply {
viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver.removeOnGlobalLayoutListener(this)
if (initialPageSelected) {
return
}
try { try {
if (DEBUG_VIEW_PAGER) Timber.i("Home pager: set initial page $selectedIndex") if (DEBUG_VIEW_PAGER) Timber.i("Home pager: set initial page $selectedIndex")
views.roomListContainerPager.setCurrentItem(selectedIndex ?: 0, false) views.roomListContainerPager.setCurrentItem(selectedIndex ?: 0, false)
@ -634,6 +643,8 @@ class HomeDetailFragment @Inject constructor(
Timber.e("Home pager: Could not set initial page after creating adapter: $e") Timber.e("Home pager: Could not set initial page after creating adapter: $e")
} }
} }
})
}
} else { } else {
// Set title, in case we missed it while paging // Set title, in case we missed it while paging
when (roomGroupingMethod) { when (roomGroupingMethod) {