ViewPager: Change how initial page is selected
Change-Id: Id1a7f29526dc6957dfed61ba016c89e9b45638a2
This commit is contained in:
parent
66f5b5c771
commit
68c9c5459d
|
@ -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,14 +626,24 @@ 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
|
||||||
try {
|
val pagerRecyclerView = views.roomListContainerPager.getChildAt(0)
|
||||||
if (DEBUG_VIEW_PAGER) Timber.i("Home pager: set initial page $selectedIndex")
|
pagerRecyclerView.apply {
|
||||||
views.roomListContainerPager.setCurrentItem(selectedIndex ?: 0, false)
|
viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
|
||||||
initialPageSelected = true
|
override fun onGlobalLayout() {
|
||||||
} catch (e: Exception) {
|
viewTreeObserver.removeOnGlobalLayoutListener(this)
|
||||||
Timber.e("Home pager: Could not set initial page after creating adapter: $e")
|
if (initialPageSelected) {
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (DEBUG_VIEW_PAGER) Timber.i("Home pager: set initial page $selectedIndex")
|
||||||
|
views.roomListContainerPager.setCurrentItem(selectedIndex ?: 0, false)
|
||||||
|
initialPageSelected = true
|
||||||
|
} catch (e: Exception) {
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue