2512: Allow more than 5 tabs (#4051)

Fixes #2512 


![grafik](https://github.com/tuskyapp/Tusky/assets/1618905/f8199d10-e26a-4f14-93c3-95cb890ea663)

Can add an arbitrary number of tabs.
Graphical behavior is unchanged for small numbers: the whole space if
filled with the tabs - they are enlarged if needed.

If there are more the mode switches to "scrollable".
This does not, however, look very differently (see screenshot with the
current tab scrolled out).

---------

Co-authored-by: Konrad Pozniak <connyduck@users.noreply.github.com>
This commit is contained in:
UlrichKu 2023-10-11 09:54:01 +02:00 committed by GitHub
commit 81a04c8977
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 27 deletions

View file

@ -0,0 +1,41 @@
package com.keylesspalace.tusky.view
import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout
/**
* Workaround for "auto" mode not behaving as expected.
*
* Switches the tab display mode depending on available size: start out with "scrollable" but
* if there is enough room switch to "fixed" (and re-measure).
*
* Idea taken from https://stackoverflow.com/a/44894143
*/
class AdaptiveTabLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : TabLayout(context, attrs, defStyleAttr) {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
tabMode = MODE_SCROLLABLE // make sure to only measure the "minimum width"
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
if (tabCount < 2) {
return
}
val tabLayout = getChildAt(0) as ViewGroup
var widthOfAllTabs = 0
for (i in 0 until tabLayout.childCount) {
widthOfAllTabs += tabLayout.getChildAt(i).measuredWidth
}
if (widthOfAllTabs <= measuredWidth) {
// fill all space if there is enough room
tabMode = MODE_FIXED
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}
}