Android: Migrate to ViewPager2
This commit is contained in:
parent
8167a046ef
commit
937d3ced8a
|
@ -131,6 +131,7 @@ dependencies {
|
||||||
implementation("androidx.core:core-splashscreen:1.0.1")
|
implementation("androidx.core:core-splashscreen:1.0.1")
|
||||||
implementation("androidx.preference:preference-ktx:1.2.1")
|
implementation("androidx.preference:preference-ktx:1.2.1")
|
||||||
implementation("androidx.profileinstaller:profileinstaller:1.3.1")
|
implementation("androidx.profileinstaller:profileinstaller:1.3.1")
|
||||||
|
implementation("androidx.viewpager2:viewpager2:1.1.0")
|
||||||
|
|
||||||
// Kotlin extensions for lifecycle components
|
// Kotlin extensions for lifecycle components
|
||||||
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
|
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
|
||||||
|
|
|
@ -3,32 +3,34 @@
|
||||||
package org.dolphinemu.dolphinemu.adapters
|
package org.dolphinemu.dolphinemu.adapters
|
||||||
|
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.FragmentManager
|
import androidx.fragment.app.FragmentActivity
|
||||||
import androidx.fragment.app.FragmentPagerAdapter
|
|
||||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
|
||||||
|
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||||
import org.dolphinemu.dolphinemu.R
|
import org.dolphinemu.dolphinemu.R
|
||||||
import org.dolphinemu.dolphinemu.ui.platform.Platform
|
import org.dolphinemu.dolphinemu.ui.platform.Platform
|
||||||
import org.dolphinemu.dolphinemu.ui.platform.PlatformGamesFragment
|
import org.dolphinemu.dolphinemu.ui.platform.PlatformGamesFragment
|
||||||
|
|
||||||
class PlatformPagerAdapter(
|
class PlatformStateAdapter(
|
||||||
fm: FragmentManager,
|
fa: FragmentActivity,
|
||||||
private val onRefreshListener: OnRefreshListener
|
private val onRefreshListener: OnRefreshListener
|
||||||
) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
|
) : FragmentStateAdapter(fa) {
|
||||||
override fun getItem(position: Int): Fragment {
|
|
||||||
|
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 platform = Platform.fromPosition(position)
|
||||||
val fragment = PlatformGamesFragment.newInstance(platform)
|
val fragment = PlatformGamesFragment.newInstance(platform)
|
||||||
fragment.setOnRefreshListener(onRefreshListener)
|
fragment.setOnRefreshListener(onRefreshListener)
|
||||||
return fragment
|
return fragment
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getCount(): Int = TAB_ICONS.size
|
override fun getItemCount(): Int = tabIcons.size
|
||||||
|
|
||||||
companion object {
|
fun getTabIcon(position: Int): Int {
|
||||||
@JvmField
|
return tabIcons[position]
|
||||||
val TAB_ICONS = intArrayOf(
|
|
||||||
R.drawable.ic_gamecube,
|
|
||||||
R.drawable.ic_wii,
|
|
||||||
R.drawable.ic_folder
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -18,9 +18,10 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
|
||||||
import com.google.android.material.appbar.AppBarLayout
|
import com.google.android.material.appbar.AppBarLayout
|
||||||
import com.google.android.material.color.MaterialColors
|
import com.google.android.material.color.MaterialColors
|
||||||
import com.google.android.material.tabs.TabLayout
|
import com.google.android.material.tabs.TabLayout
|
||||||
|
import com.google.android.material.tabs.TabLayoutMediator
|
||||||
import org.dolphinemu.dolphinemu.R
|
import org.dolphinemu.dolphinemu.R
|
||||||
import org.dolphinemu.dolphinemu.activities.EmulationActivity
|
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.databinding.ActivityMainBinding
|
||||||
import org.dolphinemu.dolphinemu.features.settings.model.IntSetting
|
import org.dolphinemu.dolphinemu.features.settings.model.IntSetting
|
||||||
import org.dolphinemu.dolphinemu.features.settings.model.NativeConfig
|
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.
|
// Don't call this before DirectoryInitialization completes.
|
||||||
private fun setPlatformTabsAndStartGameFileCacheService() {
|
private fun setPlatformTabsAndStartGameFileCacheService() {
|
||||||
val platformPagerAdapter = PlatformPagerAdapter(
|
val platformStateAdapter = PlatformStateAdapter(this, this)
|
||||||
supportFragmentManager, this
|
binding.pagerPlatforms.adapter = platformStateAdapter
|
||||||
)
|
binding.pagerPlatforms.offscreenPageLimit = platformStateAdapter.itemCount
|
||||||
binding.pagerPlatforms.adapter = platformPagerAdapter
|
TabLayoutMediator(binding.tabsPlatforms, binding.pagerPlatforms) { tab, position ->
|
||||||
binding.pagerPlatforms.offscreenPageLimit = platformPagerAdapter.count
|
tab.setIcon(platformStateAdapter.getTabIcon(position))
|
||||||
binding.tabsPlatforms.setupWithViewPager(binding.pagerPlatforms)
|
}.attach()
|
||||||
binding.tabsPlatforms.addOnTabSelectedListener(
|
binding.tabsPlatforms.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
|
||||||
object : TabLayout.ViewPagerOnTabSelectedListener(binding.pagerPlatforms) {
|
|
||||||
override fun onTabSelected(tab: TabLayout.Tab) {
|
override fun onTabSelected(tab: TabLayout.Tab) {
|
||||||
super.onTabSelected(tab)
|
IntSetting.MAIN_LAST_PLATFORM_TAB.setInt(NativeConfig.LAYER_BASE, tab.position)
|
||||||
IntSetting.MAIN_LAST_PLATFORM_TAB.setInt(
|
|
||||||
NativeConfig.LAYER_BASE,
|
|
||||||
tab.position
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
for (i in PlatformPagerAdapter.TAB_ICONS.indices) {
|
override fun onTabUnselected(tab: TabLayout.Tab) {}
|
||||||
binding.tabsPlatforms.getTabAt(i)?.setIcon(PlatformPagerAdapter.TAB_ICONS[i])
|
|
||||||
}
|
override fun onTabReselected(tab: TabLayout.Tab) {}
|
||||||
|
})
|
||||||
|
|
||||||
binding.pagerPlatforms.currentItem = IntSetting.MAIN_LAST_PLATFORM_TAB.int
|
binding.pagerPlatforms.currentItem = IntSetting.MAIN_LAST_PLATFORM_TAB.int
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
|
|
||||||
</com.google.android.material.appbar.AppBarLayout>
|
</com.google.android.material.appbar.AppBarLayout>
|
||||||
|
|
||||||
<androidx.viewpager.widget.ViewPager
|
<androidx.viewpager2.widget.ViewPager2
|
||||||
android:id="@+id/pager_platforms"
|
android:id="@+id/pager_platforms"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
|
Loading…
Reference in New Issue