show only the selected tabs
This commit is contained in:
parent
fc42309a1c
commit
8e3e5d174a
4 changed files with 91 additions and 18 deletions
|
@ -29,9 +29,7 @@ import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog
|
|||
import com.simplemobiletools.musicplayer.dialogs.SleepTimerCustomDialog
|
||||
import com.simplemobiletools.musicplayer.extensions.*
|
||||
import com.simplemobiletools.musicplayer.fragments.MyViewPagerFragment
|
||||
import com.simplemobiletools.musicplayer.helpers.INIT_QUEUE
|
||||
import com.simplemobiletools.musicplayer.helpers.START_SLEEP_TIMER
|
||||
import com.simplemobiletools.musicplayer.helpers.STOP_SLEEP_TIMER
|
||||
import com.simplemobiletools.musicplayer.helpers.*
|
||||
import com.simplemobiletools.musicplayer.models.Events
|
||||
import com.simplemobiletools.musicplayer.services.MusicService
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
|
@ -49,11 +47,13 @@ class MainActivity : SimpleActivity() {
|
|||
private var isSearchOpen = false
|
||||
private var searchMenuItem: MenuItem? = null
|
||||
private var bus: EventBus? = null
|
||||
private var storedShowTabs = 0
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
appLaunched(BuildConfig.APPLICATION_ID)
|
||||
storeStateVariables()
|
||||
|
||||
handlePermission(PERMISSION_WRITE_STORAGE) {
|
||||
if (it) {
|
||||
|
@ -75,6 +75,12 @@ class MainActivity : SimpleActivity() {
|
|||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (storedShowTabs != config.showTabs) {
|
||||
config.lastUsedViewPagerPage = 0
|
||||
System.exit(0)
|
||||
return
|
||||
}
|
||||
|
||||
updateTextColors(main_holder)
|
||||
sleep_timer_holder.background = ColorDrawable(config.backgroundColor)
|
||||
sleep_timer_stop.applyColorFilter(config.textColor)
|
||||
|
@ -93,6 +99,7 @@ class MainActivity : SimpleActivity() {
|
|||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
storeStateVariables()
|
||||
config.lastUsedViewPagerPage = viewpager.currentItem
|
||||
}
|
||||
|
||||
|
@ -131,6 +138,12 @@ class MainActivity : SimpleActivity() {
|
|||
return true
|
||||
}
|
||||
|
||||
private fun storeStateVariables() {
|
||||
config.apply {
|
||||
storedShowTabs = showTabs
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupSearch(menu: Menu) {
|
||||
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
|
||||
searchMenuItem = menu.findItem(R.id.search)
|
||||
|
@ -187,7 +200,7 @@ class MainActivity : SimpleActivity() {
|
|||
|
||||
private fun initFragments() {
|
||||
viewpager.adapter = ViewPagerAdapter(this)
|
||||
viewpager.offscreenPageLimit = 4
|
||||
viewpager.offscreenPageLimit = tabsList.size - 1
|
||||
viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
||||
override fun onPageScrollStateChanged(state: Int) {
|
||||
if (isSearchOpen) {
|
||||
|
@ -218,14 +231,17 @@ class MainActivity : SimpleActivity() {
|
|||
getString(R.string.albums),
|
||||
getString(R.string.tracks)
|
||||
)
|
||||
main_tabs_holder.apply {
|
||||
removeAllTabs()
|
||||
|
||||
for (i in tabLabels.indices) {
|
||||
val label = tabLabels[i]
|
||||
val tab = newTab().setText(label)
|
||||
main_tabs_holder.removeAllTabs()
|
||||
var skippedTabs = 0
|
||||
tabsList.forEachIndexed { index, value ->
|
||||
if (config.showTabs and value == 0) {
|
||||
skippedTabs++
|
||||
} else {
|
||||
val label = tabLabels[index]
|
||||
val tab = main_tabs_holder.newTab().setText(label)
|
||||
tab.contentDescription = label
|
||||
addTab(tab, i, i == 0)
|
||||
main_tabs_holder.addTab(tab, index - skippedTabs, config.lastUsedViewPagerPage == index - skippedTabs)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,8 +8,11 @@ import com.simplemobiletools.musicplayer.R
|
|||
import com.simplemobiletools.musicplayer.activities.SimpleActivity
|
||||
import com.simplemobiletools.musicplayer.extensions.config
|
||||
import com.simplemobiletools.musicplayer.fragments.MyViewPagerFragment
|
||||
import com.simplemobiletools.musicplayer.helpers.*
|
||||
|
||||
class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
|
||||
val showTabs = activity.config.showTabs
|
||||
|
||||
override fun instantiateItem(container: ViewGroup, position: Int): Any {
|
||||
val layout = getFragment(position)
|
||||
val view = activity.layoutInflater.inflate(layout, container, false)
|
||||
|
@ -27,15 +30,32 @@ class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
|
|||
container.removeView(item as View)
|
||||
}
|
||||
|
||||
override fun getCount() = 5
|
||||
override fun getCount() = tabsList.filter { it and showTabs != 0 }.size
|
||||
|
||||
override fun isViewFromObject(view: View, item: Any) = view == item
|
||||
|
||||
private fun getFragment(position: Int) = when (position) {
|
||||
0 -> R.layout.fragment_playlists
|
||||
1 -> R.layout.fragment_folders
|
||||
2 -> R.layout.fragment_artists
|
||||
3 -> R.layout.fragment_albums
|
||||
else -> R.layout.fragment_tracks
|
||||
private fun getFragment(position: Int): Int {
|
||||
val fragments = arrayListOf<Int>()
|
||||
if (showTabs and TAB_PLAYLISTS != 0) {
|
||||
fragments.add(R.layout.fragment_playlists)
|
||||
}
|
||||
|
||||
if (showTabs and TAB_FOLDERS != 0) {
|
||||
fragments.add(R.layout.fragment_folders)
|
||||
}
|
||||
|
||||
if (showTabs and TAB_ARTISTS != 0) {
|
||||
fragments.add(R.layout.fragment_artists)
|
||||
}
|
||||
|
||||
if (showTabs and TAB_ALBUMS != 0) {
|
||||
fragments.add(R.layout.fragment_albums)
|
||||
}
|
||||
|
||||
if (showTabs and TAB_TRACKS != 0) {
|
||||
fragments.add(R.layout.fragment_tracks)
|
||||
}
|
||||
|
||||
return fragments[position]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package com.simplemobiletools.musicplayer.views
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.ViewGroup
|
||||
import com.google.android.material.tabs.TabLayout
|
||||
|
||||
// make the tabLayout scrollable horizotally when needed, else make sure it fills whole width
|
||||
class MyTabLayout : TabLayout {
|
||||
constructor(context: Context?) : super(context!!)
|
||||
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs)
|
||||
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context!!, attrs, defStyleAttr)
|
||||
|
||||
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
|
||||
try {
|
||||
if (tabCount == 0) {
|
||||
return
|
||||
}
|
||||
|
||||
val tabLayout = getChildAt(0) as ViewGroup
|
||||
val childCount: Int = tabLayout.childCount
|
||||
val widths = IntArray(childCount + 1)
|
||||
|
||||
for (i in 0 until childCount) {
|
||||
widths[i] = tabLayout.getChildAt(i).measuredWidth
|
||||
widths[childCount] += widths[i]
|
||||
}
|
||||
|
||||
val measuredWidth = measuredWidth
|
||||
for (i in 0 until childCount) {
|
||||
tabLayout.getChildAt(i).minimumWidth = measuredWidth * widths[i] / widths[childCount]
|
||||
}
|
||||
} catch (ignored: Exception) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.google.android.material.tabs.TabLayout
|
||||
<com.simplemobiletools.musicplayer.views.MyTabLayout
|
||||
android:id="@+id/main_tabs_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="48dp"
|
||||
|
|
Loading…
Reference in a new issue