Android: Migrate to ViewPager2

This commit is contained in:
TacoTheDank 2024-05-26 02:43:57 -04:00
parent 8167a046ef
commit 937d3ced8a
4 changed files with 34 additions and 35 deletions

View File

@ -131,6 +131,7 @@ dependencies {
implementation("androidx.core:core-splashscreen:1.0.1")
implementation("androidx.preference:preference-ktx:1.2.1")
implementation("androidx.profileinstaller:profileinstaller:1.3.1")
implementation("androidx.viewpager2:viewpager2:1.1.0")
// Kotlin extensions for lifecycle components
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")

View File

@ -3,32 +3,34 @@
package org.dolphinemu.dolphinemu.adapters
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.FragmentActivity
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
import androidx.viewpager2.adapter.FragmentStateAdapter
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.ui.platform.Platform
import org.dolphinemu.dolphinemu.ui.platform.PlatformGamesFragment
class PlatformPagerAdapter(
fm: FragmentManager,
class PlatformStateAdapter(
fa: FragmentActivity,
private val onRefreshListener: OnRefreshListener
) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment {
) : FragmentStateAdapter(fa) {
private val tabIcons = intArrayOf(
R.drawable.ic_gamecube,
R.drawable.ic_wii,
R.drawable.ic_folder
)
override fun createFragment(position: Int): Fragment {
val platform = Platform.fromPosition(position)
val fragment = PlatformGamesFragment.newInstance(platform)
fragment.setOnRefreshListener(onRefreshListener)
return fragment
}
override fun getCount(): Int = TAB_ICONS.size
override fun getItemCount(): Int = tabIcons.size
companion object {
@JvmField
val TAB_ICONS = intArrayOf(
R.drawable.ic_gamecube,
R.drawable.ic_wii,
R.drawable.ic_folder
)
fun getTabIcon(position: Int): Int {
return tabIcons[position]
}
}

View File

@ -18,9 +18,10 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.color.MaterialColors
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.activities.EmulationActivity
import org.dolphinemu.dolphinemu.adapters.PlatformPagerAdapter
import org.dolphinemu.dolphinemu.adapters.PlatformStateAdapter
import org.dolphinemu.dolphinemu.databinding.ActivityMainBinding
import org.dolphinemu.dolphinemu.features.settings.model.IntSetting
import org.dolphinemu.dolphinemu.features.settings.model.NativeConfig
@ -282,26 +283,21 @@ class MainActivity : AppCompatActivity(), MainView, OnRefreshListener, ThemeProv
// Don't call this before DirectoryInitialization completes.
private fun setPlatformTabsAndStartGameFileCacheService() {
val platformPagerAdapter = PlatformPagerAdapter(
supportFragmentManager, this
)
binding.pagerPlatforms.adapter = platformPagerAdapter
binding.pagerPlatforms.offscreenPageLimit = platformPagerAdapter.count
binding.tabsPlatforms.setupWithViewPager(binding.pagerPlatforms)
binding.tabsPlatforms.addOnTabSelectedListener(
object : TabLayout.ViewPagerOnTabSelectedListener(binding.pagerPlatforms) {
override fun onTabSelected(tab: TabLayout.Tab) {
super.onTabSelected(tab)
IntSetting.MAIN_LAST_PLATFORM_TAB.setInt(
NativeConfig.LAYER_BASE,
tab.position
)
}
})
val platformStateAdapter = PlatformStateAdapter(this, this)
binding.pagerPlatforms.adapter = platformStateAdapter
binding.pagerPlatforms.offscreenPageLimit = platformStateAdapter.itemCount
TabLayoutMediator(binding.tabsPlatforms, binding.pagerPlatforms) { tab, position ->
tab.setIcon(platformStateAdapter.getTabIcon(position))
}.attach()
binding.tabsPlatforms.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
IntSetting.MAIN_LAST_PLATFORM_TAB.setInt(NativeConfig.LAYER_BASE, tab.position)
}
for (i in PlatformPagerAdapter.TAB_ICONS.indices) {
binding.tabsPlatforms.getTabAt(i)?.setIcon(PlatformPagerAdapter.TAB_ICONS[i])
}
override fun onTabUnselected(tab: TabLayout.Tab) {}
override fun onTabReselected(tab: TabLayout.Tab) {}
})
binding.pagerPlatforms.currentItem = IntSetting.MAIN_LAST_PLATFORM_TAB.int

View File

@ -35,7 +35,7 @@
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager_platforms"
android:layout_width="match_parent"
android:layout_height="match_parent"