mirror of
https://github.com/jrpie/Launcher.git
synced 2025-04-11 23:04:32 +02:00
replace (ViewPager, FragmentPagerAdapter) by (ViewPager2, FragmentStateAdapter)
This commit is contained in:
parent
47940811b4
commit
90434617e7
4 changed files with 40 additions and 41 deletions
|
@ -12,10 +12,8 @@ import android.window.OnBackInvokedDispatcher
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.content.res.AppCompatResources
|
import androidx.appcompat.content.res.AppCompatResources
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.FragmentManager
|
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||||
import androidx.fragment.app.FragmentPagerAdapter
|
import com.google.android.material.tabs.TabLayoutMediator
|
||||||
import androidx.viewpager.widget.ViewPager
|
|
||||||
import com.google.android.material.tabs.TabLayout
|
|
||||||
import de.jrpie.android.launcher.Application
|
import de.jrpie.android.launcher.Application
|
||||||
import de.jrpie.android.launcher.R
|
import de.jrpie.android.launcher.R
|
||||||
import de.jrpie.android.launcher.REQUEST_UNINSTALL
|
import de.jrpie.android.launcher.REQUEST_UNINSTALL
|
||||||
|
@ -241,11 +239,14 @@ class ListActivity : AppCompatActivity(), UIObject {
|
||||||
|
|
||||||
updateTitle()
|
updateTitle()
|
||||||
|
|
||||||
val sectionsPagerAdapter = ListSectionsPagerAdapter(this, supportFragmentManager)
|
val sectionsPagerAdapter = ListSectionsPagerAdapter(this)
|
||||||
val viewPager: ViewPager = findViewById(R.id.list_viewpager)
|
binding.listViewpager.apply {
|
||||||
viewPager.adapter = sectionsPagerAdapter
|
adapter = sectionsPagerAdapter
|
||||||
val tabs: TabLayout = findViewById(R.id.list_tabs)
|
currentItem = 0
|
||||||
tabs.setupWithViewPager(viewPager)
|
}
|
||||||
|
TabLayoutMediator(binding.listTabs, binding.listViewpager) { tab, position ->
|
||||||
|
tab.text = sectionsPagerAdapter.getPageTitle(position)
|
||||||
|
}.attach()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,10 +259,10 @@ private val TAB_TITLES = arrayOf(
|
||||||
* The [ListSectionsPagerAdapter] returns the fragment,
|
* The [ListSectionsPagerAdapter] returns the fragment,
|
||||||
* which corresponds to the selected tab in [ListActivity].
|
* which corresponds to the selected tab in [ListActivity].
|
||||||
*/
|
*/
|
||||||
class ListSectionsPagerAdapter(private val activity: ListActivity, fm: FragmentManager) :
|
class ListSectionsPagerAdapter(private val activity: ListActivity) :
|
||||||
FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
|
FragmentStateAdapter(activity) {
|
||||||
|
|
||||||
override fun getItem(position: Int): Fragment {
|
override fun createFragment(position: Int): Fragment {
|
||||||
return when (position) {
|
return when (position) {
|
||||||
0 -> ListFragmentApps()
|
0 -> ListFragmentApps()
|
||||||
1 -> ListFragmentOther()
|
1 -> ListFragmentOther()
|
||||||
|
@ -269,11 +270,11 @@ class ListSectionsPagerAdapter(private val activity: ListActivity, fm: FragmentM
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getPageTitle(position: Int): CharSequence {
|
fun getPageTitle(position: Int): CharSequence {
|
||||||
return activity.resources.getString(TAB_TITLES[position])
|
return activity.resources.getString(TAB_TITLES[position])
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getCount(): Int {
|
override fun getItemCount(): Int {
|
||||||
return when (activity.intention) {
|
return when (activity.intention) {
|
||||||
ListActivity.ListActivityIntention.VIEW -> 1
|
ListActivity.ListActivityIntention.VIEW -> 1
|
||||||
else -> 2
|
else -> 2
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package de.jrpie.android.launcher.ui.settings
|
package de.jrpie.android.launcher.ui.settings
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
|
@ -8,10 +7,9 @@ import android.os.Bundle
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
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.viewpager2.adapter.FragmentStateAdapter
|
||||||
import androidx.viewpager.widget.ViewPager
|
import com.google.android.material.tabs.TabLayoutMediator
|
||||||
import com.google.android.material.tabs.TabLayout
|
|
||||||
import de.jrpie.android.launcher.R
|
import de.jrpie.android.launcher.R
|
||||||
import de.jrpie.android.launcher.REQUEST_CHOOSE_APP
|
import de.jrpie.android.launcher.REQUEST_CHOOSE_APP
|
||||||
import de.jrpie.android.launcher.databinding.SettingsBinding
|
import de.jrpie.android.launcher.databinding.SettingsBinding
|
||||||
|
@ -34,6 +32,7 @@ import de.jrpie.android.launcher.ui.settings.meta.SettingsFragmentMeta
|
||||||
* Settings are closed automatically if the activity goes `onPause` unexpectedly.
|
* Settings are closed automatically if the activity goes `onPause` unexpectedly.
|
||||||
*/
|
*/
|
||||||
class SettingsActivity : AppCompatActivity(), UIObject {
|
class SettingsActivity : AppCompatActivity(), UIObject {
|
||||||
|
private val EXTRA_TAB = "tab"
|
||||||
|
|
||||||
private val solidBackground = LauncherPreferences.theme().background() == Background.SOLID
|
private val solidBackground = LauncherPreferences.theme().background() == Background.SOLID
|
||||||
|| LauncherPreferences.theme().colorTheme() == ColorTheme.LIGHT
|
|| LauncherPreferences.theme().colorTheme() == ColorTheme.LIGHT
|
||||||
|
@ -49,15 +48,15 @@ class SettingsActivity : AppCompatActivity(), UIObject {
|
||||||
// This ugly workaround causes a jump to the top of the list, but at least
|
// This ugly workaround causes a jump to the top of the list, but at least
|
||||||
// the text stays readable.
|
// the text stays readable.
|
||||||
val i = Intent(this, SettingsActivity::class.java)
|
val i = Intent(this, SettingsActivity::class.java)
|
||||||
.also { it.putExtra("tab", 1) }
|
.also { it.putExtra(EXTRA_TAB, 1) }
|
||||||
finish()
|
finish()
|
||||||
startActivity(i)
|
startActivity(i)
|
||||||
} else
|
} else
|
||||||
if (prefKey?.startsWith("theme.") == true ||
|
if (prefKey?.startsWith("theme.") == true ||
|
||||||
prefKey?.startsWith("display.") == true
|
prefKey?.startsWith("display.") == true
|
||||||
) {
|
) {
|
||||||
recreate()
|
recreate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private lateinit var binding: SettingsBinding
|
private lateinit var binding: SettingsBinding
|
||||||
|
|
||||||
|
@ -71,15 +70,14 @@ class SettingsActivity : AppCompatActivity(), UIObject {
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
// set up tabs and swiping in settings
|
// set up tabs and swiping in settings
|
||||||
val sectionsPagerAdapter = SettingsSectionsPagerAdapter(this, supportFragmentManager)
|
val sectionsPagerAdapter = SettingsSectionsPagerAdapter(this)
|
||||||
val viewPager: ViewPager = findViewById(R.id.settings_viewpager)
|
binding.settingsViewpager.apply {
|
||||||
viewPager.adapter = sectionsPagerAdapter
|
adapter = sectionsPagerAdapter
|
||||||
|
setCurrentItem(intent.getIntExtra(EXTRA_TAB, 0), false)
|
||||||
val tabs: TabLayout = findViewById(R.id.settings_tabs)
|
|
||||||
tabs.setupWithViewPager(viewPager)
|
|
||||||
if (intent.hasExtra("tab")) {
|
|
||||||
tabs.getTabAt(intent.getIntExtra("tab", 0))?.select()
|
|
||||||
}
|
}
|
||||||
|
TabLayoutMediator(binding.settingsTabs, binding.settingsViewpager) { tab, position ->
|
||||||
|
tab.text = sectionsPagerAdapter.getPageTitle(position)
|
||||||
|
}.attach()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
|
@ -122,10 +120,10 @@ private val TAB_TITLES = arrayOf(
|
||||||
R.string.settings_tab_meta
|
R.string.settings_tab_meta
|
||||||
)
|
)
|
||||||
|
|
||||||
class SettingsSectionsPagerAdapter(private val context: Context, fm: FragmentManager) :
|
class SettingsSectionsPagerAdapter(private val activity: FragmentActivity) :
|
||||||
FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
|
FragmentStateAdapter(activity) {
|
||||||
|
|
||||||
override fun getItem(position: Int): Fragment {
|
override fun createFragment(position: Int): Fragment {
|
||||||
return when (position) {
|
return when (position) {
|
||||||
0 -> SettingsFragmentActions()
|
0 -> SettingsFragmentActions()
|
||||||
1 -> SettingsFragmentLauncher()
|
1 -> SettingsFragmentLauncher()
|
||||||
|
@ -134,11 +132,11 @@ class SettingsSectionsPagerAdapter(private val context: Context, fm: FragmentMan
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getPageTitle(position: Int): CharSequence {
|
fun getPageTitle(position: Int): CharSequence {
|
||||||
return context.resources.getString(TAB_TITLES[position])
|
return activity.resources.getString(TAB_TITLES[position])
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getCount(): Int {
|
override fun getItemCount(): Int {
|
||||||
return 3
|
return 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,7 +95,7 @@
|
||||||
|
|
||||||
</com.google.android.material.appbar.AppBarLayout>
|
</com.google.android.material.appbar.AppBarLayout>
|
||||||
|
|
||||||
<androidx.viewpager.widget.ViewPager
|
<androidx.viewpager2.widget.ViewPager2
|
||||||
android:id="@+id/list_viewpager"
|
android:id="@+id/list_viewpager"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
|
|
|
@ -73,7 +73,7 @@
|
||||||
|
|
||||||
</com.google.android.material.appbar.AppBarLayout>
|
</com.google.android.material.appbar.AppBarLayout>
|
||||||
|
|
||||||
<androidx.viewpager.widget.ViewPager
|
<androidx.viewpager2.widget.ViewPager2
|
||||||
android:id="@+id/settings_viewpager"
|
android:id="@+id/settings_viewpager"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
|
Loading…
Add table
Reference in a new issue