-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow-up to #1232: Fix crash on device rotation
Migrated from ViewPager to ViewPager2
- Loading branch information
1 parent
78622c5
commit 943c92c
Showing
7 changed files
with
311 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
myExpenses/src/main/java/org/totschnig/myexpenses/activity/TabbedActivity.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
myExpenses/src/main/java/org/totschnig/myexpenses/activity/TabbedActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.totschnig.myexpenses.activity | ||
|
||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.viewpager2.adapter.FragmentStateAdapter | ||
import com.google.android.material.tabs.TabLayoutMediator | ||
import org.totschnig.myexpenses.databinding.ActivityWithTabsBinding | ||
import org.totschnig.myexpenses.ui.FragmentPagerAdapter | ||
|
||
abstract class TabbedActivity : ProtectedFragmentActivity() { | ||
lateinit var binding: ActivityWithTabsBinding | ||
|
||
lateinit var mSectionsPagerAdapter: SectionsPagerAdapter | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivityWithTabsBinding.inflate( | ||
layoutInflater | ||
) | ||
setContentView(binding.root) | ||
setupToolbar() | ||
mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager) | ||
binding.viewPager.adapter = mSectionsPagerAdapter | ||
TabLayoutMediator(binding.tabs, binding.viewPager) { tab, position -> | ||
tab.text = getTitle(position) | ||
}.attach() | ||
} | ||
|
||
abstract fun getTitle(position: Int): CharSequence | ||
|
||
abstract fun createFragment(position: Int): Fragment | ||
|
||
abstract fun getItemCount(): Int | ||
|
||
/** | ||
* A [FragmentPagerAdapter] that returns a fragment corresponding to | ||
* one of the sections/tabs/pages. | ||
*/ | ||
inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentStateAdapter(fm, lifecycle) { | ||
|
||
fun getFragmentName(currentPosition: Int): String { | ||
//https://stackoverflow.com/a/61178226/1199911 | ||
return "f" + getItemId(currentPosition) | ||
} | ||
|
||
override fun getItemCount() = this@TabbedActivity.getItemCount() | ||
|
||
override fun createFragment(position: Int) = this@TabbedActivity.createFragment(position) | ||
} | ||
|
||
override val snackBarContainerId: Int | ||
get() = binding.viewPager.id | ||
} |
Oops, something went wrong.