From 81da9ea456dbb9d471c34e8c7317dd96ac26ced2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 31 Mar 2020 20:22:04 +0200 Subject: [PATCH] adding a player fragment --- .../voicerecorder/activities/MainActivity.kt | 31 ++++++++++--------- .../adapters/ViewPagerAdapter.kt | 12 ++++--- .../voicerecorder/fragments/PlayerFragment.kt | 11 +++++++ app/src/main/res/layout/fragment_player.xml | 7 +++++ 4 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt create mode 100644 app/src/main/res/layout/fragment_player.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt index 14c934f..0024ba9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt @@ -34,13 +34,12 @@ class MainActivity : SimpleActivity() { finish() } } - - setupTabColors(config.lastUsedViewPagerPage) } override fun onResume() { super.onResume() getPagerAdapter()?.onResume() + setupTabColors() } override fun onDestroy() { @@ -84,19 +83,10 @@ class MainActivity : SimpleActivity() { private fun setupViewPager() { view_pager.adapter = ViewPagerAdapter(this) - } - - private fun getPagerAdapter() = (view_pager.adapter as? ViewPagerAdapter) - - private fun setupTabColors(lastUsedTab: Int) { - main_tabs_holder.apply { - background = ColorDrawable(config.backgroundColor) - setSelectedTabIndicatorColor(getAdjustedPrimaryColor()) - getTabAt(lastUsedTab)?.apply { - select() - icon?.applyColorFilter(getAdjustedPrimaryColor()) - } + view_pager.onPageChangeListener { + main_tabs_holder.getTabAt(it)?.select() } + view_pager.currentItem = config.lastUsedViewPagerPage main_tabs_holder.onTabSelectionChanged( tabUnselectedAction = { @@ -109,6 +99,19 @@ class MainActivity : SimpleActivity() { ) } + private fun getPagerAdapter() = (view_pager.adapter as? ViewPagerAdapter) + + private fun setupTabColors() { + main_tabs_holder.apply { + background = ColorDrawable(config.backgroundColor) + setSelectedTabIndicatorColor(getAdjustedPrimaryColor()) + getTabAt(view_pager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor()) + getTabAt(getInactiveTabIndex())?.icon?.applyColorFilter(config.textColor) + } + } + + private fun getInactiveTabIndex() = if (view_pager.currentItem == 0) 1 else 0 + private fun launchSettings() { startActivity(Intent(applicationContext, SettingsActivity::class.java)) } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/adapters/ViewPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/adapters/ViewPagerAdapter.kt index ced0ffb..ef628e2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/adapters/ViewPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/adapters/ViewPagerAdapter.kt @@ -12,7 +12,7 @@ class ViewPagerAdapter(private val activity: SimpleActivity) : PagerAdapter() { private val mFragments = SparseArray() override fun instantiateItem(container: ViewGroup, position: Int): Any { - val layout = R.layout.fragment_recorder + val layout = if (position == 0) R.layout.fragment_recorder else R.layout.fragment_player val view = activity.layoutInflater.inflate(layout, container, false) container.addView(view) @@ -25,15 +25,19 @@ class ViewPagerAdapter(private val activity: SimpleActivity) : PagerAdapter() { container.removeView(item as View) } - override fun getCount() = 1 + override fun getCount() = 2 override fun isViewFromObject(view: View, item: Any) = view == item fun onResume() { - mFragments.get(0)?.onResume() + for (i in 0 until mFragments.size()) { + mFragments[i].onResume() + } } fun onDestroy() { - mFragments.get(0)?.onDestroy() + for (i in 0 until mFragments.size()) { + mFragments[i].onDestroy() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt new file mode 100644 index 0000000..bdf5d55 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/PlayerFragment.kt @@ -0,0 +1,11 @@ +package com.simplemobiletools.voicerecorder.fragments + +import android.content.Context +import android.util.AttributeSet + +class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { + + override fun onResume() {} + + override fun onDestroy() {} +} diff --git a/app/src/main/res/layout/fragment_player.xml b/app/src/main/res/layout/fragment_player.xml new file mode 100644 index 0000000..9bfa085 --- /dev/null +++ b/app/src/main/res/layout/fragment_player.xml @@ -0,0 +1,7 @@ + + + +