|
|
@@ -9,10 +9,9 @@ import android.util.TypedValue
|
|
|
import android.view.GestureDetector
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.MotionEvent
|
|
|
-import android.view.View
|
|
|
-import android.view.ViewGroup
|
|
|
import android.widget.LinearLayout
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout
|
|
|
+import androidx.core.view.updateLayoutParams
|
|
|
import androidx.viewpager2.widget.ViewPager2
|
|
|
import com.adealink.frame.aab.util.getCompatColor
|
|
|
import com.adealink.frame.util.DisplayUtil
|
|
|
@@ -39,6 +38,11 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
private const val MODE_AUTO = 2
|
|
|
private const val TYPE_WRAP_CONTENT = 0
|
|
|
private const val TYPE_MATCH_PARENT = 1
|
|
|
+
|
|
|
+
|
|
|
+ private const val INDICATOR_WIDTH_MATCH_PARENT = 0 //铺满整个tab
|
|
|
+ private const val INDICATOR_WIDTH_WRAP_TEXT = 1 //包裹文字
|
|
|
+ private const val INDICATOR_WIDTH_FIXED = 2 //固定大小
|
|
|
}
|
|
|
|
|
|
private var binding: LayoutCommonTabLayoutBinding =
|
|
|
@@ -50,6 +54,13 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
private var normalTabTextSize = 16f.dpf()
|
|
|
private var selectedTabTextSize = 16f.dpf()
|
|
|
private var indicatorColor: Int = 0
|
|
|
+ private var indicatorDrawable: Drawable? = null
|
|
|
+
|
|
|
+ private var indicatorWidthMode: Int = INDICATOR_WIDTH_MATCH_PARENT
|
|
|
+
|
|
|
+ private var indicatorWidth: Float? = null //仅INDICATOR_WIDTH_FIXED生效
|
|
|
+
|
|
|
+ private var indicatorHeight: Float = 2.dpf()
|
|
|
private var selectedTabColor: Int = 0
|
|
|
private var selectedTabBg: Drawable? = null
|
|
|
private var normalTabColor: Int = 0
|
|
|
@@ -57,7 +68,7 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
private var tabPaddingHorizontal: Int = 4.dp()
|
|
|
private var tabPaddingVertical: Int = 0
|
|
|
private var fixedTabScrollableMode: Boolean = false
|
|
|
- private var scrollToTop: (()->Unit)? = null
|
|
|
+ private var scrollToTop: (() -> Unit)? = null
|
|
|
private val gestureDetector: GestureDetector by lazy {
|
|
|
GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
|
|
|
override fun onDoubleTap(e: MotionEvent): Boolean {
|
|
|
@@ -94,6 +105,21 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
R.styleable.CommonTabLayout_indicator_color,
|
|
|
getCompatColor(R.color.color_222222)
|
|
|
)
|
|
|
+ indicatorDrawable = typedArray.getDrawable(
|
|
|
+ R.styleable.CommonTabLayout_indicator_drawable
|
|
|
+ )
|
|
|
+ indicatorWidthMode = typedArray.getInt(
|
|
|
+ R.styleable.CommonTabLayout_indicator_width_mode,
|
|
|
+ INDICATOR_WIDTH_MATCH_PARENT
|
|
|
+ )
|
|
|
+ indicatorWidth = typedArray.getDimension(
|
|
|
+ R.styleable.CommonTabLayout_indicator_width,
|
|
|
+ 0f,
|
|
|
+ )
|
|
|
+ indicatorHeight = typedArray.getDimension(
|
|
|
+ R.styleable.CommonTabLayout_indicator_height,
|
|
|
+ 1.dpf(),
|
|
|
+ )
|
|
|
normalTabColor = typedArray.getColor(
|
|
|
R.styleable.CommonTabLayout_normal_tab_color,
|
|
|
getCompatColor(R.color.color_tab_normal_match_parent)
|
|
|
@@ -149,7 +175,7 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
totalTabWidth += it.width
|
|
|
|
|
|
// 为每个 TabItem 设置固定间距
|
|
|
- val layoutParams = it.layoutParams as ViewGroup.MarginLayoutParams
|
|
|
+ val layoutParams = it.layoutParams as MarginLayoutParams
|
|
|
layoutParams.setMargins(0, 0, spacing, 0)
|
|
|
it.layoutParams = layoutParams
|
|
|
}
|
|
|
@@ -176,7 +202,11 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
this.indicatorColor = indicatorColor
|
|
|
for (i in 0 until tabLayout.tabCount) {
|
|
|
tabLayout.getTabAt(i)?.let {
|
|
|
- updateTabView(it, viewPager2?.currentItem == i, tabFragmentStateAdapter?.getTabName(i))
|
|
|
+ updateTabView(
|
|
|
+ it,
|
|
|
+ viewPager2?.currentItem == i,
|
|
|
+ tabFragmentStateAdapter?.getTabName(i)
|
|
|
+ )
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -232,9 +262,9 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
private fun setRootWidthWithTabType(type: Int) {
|
|
|
val lp = binding.root.layoutParams
|
|
|
if (type == TYPE_WRAP_CONTENT) {
|
|
|
- lp.width = ViewGroup.LayoutParams.WRAP_CONTENT
|
|
|
+ lp.width = LayoutParams.WRAP_CONTENT
|
|
|
} else {
|
|
|
- lp.width = ViewGroup.LayoutParams.MATCH_PARENT
|
|
|
+ lp.width = LayoutParams.MATCH_PARENT
|
|
|
}
|
|
|
binding.root.layoutParams = lp
|
|
|
}
|
|
|
@@ -293,7 +323,7 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
name: String? = null,
|
|
|
showIndicator: Boolean = true,
|
|
|
) {
|
|
|
- val indicatorBackground = DrawableBuilder()
|
|
|
+ val indicatorBackground = indicatorDrawable ?: DrawableBuilder()
|
|
|
.rectangle()
|
|
|
.solidColor(indicatorColor)
|
|
|
.cornerRadius(DisplayUtil.dp2px(5f))
|
|
|
@@ -301,10 +331,43 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
if (tabType == TYPE_WRAP_CONTENT) {
|
|
|
tab?.customView?.let {
|
|
|
val customViewBinding = LayoutCommonTabItemBinding.bind(it)
|
|
|
- customViewBinding.tabTitle.setPadding(tabPaddingHorizontal, tabPaddingVertical, tabPaddingHorizontal, tabPaddingVertical)
|
|
|
- name?.let { text ->
|
|
|
- customViewBinding.tabTitle.text = text
|
|
|
- customViewBinding.vLine.background = indicatorBackground
|
|
|
+ customViewBinding.tabTitle.setPadding(
|
|
|
+ tabPaddingHorizontal,
|
|
|
+ tabPaddingVertical,
|
|
|
+ tabPaddingHorizontal,
|
|
|
+ tabPaddingVertical
|
|
|
+ )
|
|
|
+ when (indicatorWidthMode) {
|
|
|
+ INDICATOR_WIDTH_MATCH_PARENT -> {
|
|
|
+ customViewBinding.vLine.updateLayoutParams<LayoutParams> {
|
|
|
+ width = 0
|
|
|
+ height = indicatorHeight.toInt()
|
|
|
+ startToStart = LayoutParams.PARENT_ID
|
|
|
+ endToEnd = LayoutParams.PARENT_ID
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ INDICATOR_WIDTH_WRAP_TEXT -> {
|
|
|
+ customViewBinding.vLine.updateLayoutParams<LayoutParams> {
|
|
|
+ width = 0
|
|
|
+ height = indicatorHeight.toInt()
|
|
|
+ startToStart = customViewBinding.tabTitle.id
|
|
|
+ endToEnd = customViewBinding.tabTitle.id
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ INDICATOR_WIDTH_FIXED -> {
|
|
|
+ customViewBinding.vLine.updateLayoutParams<LayoutParams> {
|
|
|
+ width = indicatorWidth?.toInt() ?: 10.dp()
|
|
|
+ height = indicatorHeight.toInt()
|
|
|
+ startToStart = LayoutParams.PARENT_ID
|
|
|
+ endToEnd = LayoutParams.PARENT_ID
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ customViewBinding.vLine.background = indicatorBackground
|
|
|
+ name?.let {
|
|
|
+ customViewBinding.tabTitle.text = name
|
|
|
}
|
|
|
if (isSelected) {
|
|
|
customViewBinding.tabTitle.background = selectedTabBg
|
|
|
@@ -315,9 +378,9 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
customViewBinding.tabTitle.setTextColor(selectedTabColor)
|
|
|
customViewBinding.tabTitle.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
|
|
|
if (showIndicator) {
|
|
|
- customViewBinding.vLine.visibility = View.VISIBLE
|
|
|
+ customViewBinding.vLine.visibility = VISIBLE
|
|
|
} else {
|
|
|
- customViewBinding.vLine.visibility = View.GONE
|
|
|
+ customViewBinding.vLine.visibility = GONE
|
|
|
}
|
|
|
} else {
|
|
|
customViewBinding.tabTitle.background = normalTabBg
|
|
|
@@ -327,16 +390,49 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
)
|
|
|
customViewBinding.tabTitle.setTextColor(normalTabColor)
|
|
|
customViewBinding.tabTitle.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
|
|
|
- customViewBinding.vLine.visibility = View.GONE
|
|
|
+ customViewBinding.vLine.visibility = GONE
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
tab?.customView?.let {
|
|
|
val customViewBinding = LayoutCommonTabItemMatchParentBinding.bind(it)
|
|
|
- customViewBinding.tabTitle.setPadding(tabPaddingHorizontal, tabPaddingVertical, tabPaddingHorizontal, tabPaddingVertical)
|
|
|
- name?.let { text ->
|
|
|
- customViewBinding.tabTitle.text = text
|
|
|
- customViewBinding.vLine.background = indicatorBackground
|
|
|
+ customViewBinding.tabTitle.setPadding(
|
|
|
+ tabPaddingHorizontal,
|
|
|
+ tabPaddingVertical,
|
|
|
+ tabPaddingHorizontal,
|
|
|
+ tabPaddingVertical
|
|
|
+ )
|
|
|
+ when (indicatorWidthMode) {
|
|
|
+ INDICATOR_WIDTH_MATCH_PARENT -> {
|
|
|
+ customViewBinding.vLine.updateLayoutParams<LayoutParams> {
|
|
|
+ width = 0
|
|
|
+ height = indicatorHeight.toInt()
|
|
|
+ startToStart = LayoutParams.PARENT_ID
|
|
|
+ endToEnd = LayoutParams.PARENT_ID
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ INDICATOR_WIDTH_WRAP_TEXT -> {
|
|
|
+ customViewBinding.vLine.updateLayoutParams<LayoutParams> {
|
|
|
+ width = 0
|
|
|
+ height = indicatorHeight.toInt()
|
|
|
+ startToStart = customViewBinding.tabTitle.id
|
|
|
+ endToEnd = customViewBinding.tabTitle.id
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ INDICATOR_WIDTH_FIXED -> {
|
|
|
+ customViewBinding.vLine.updateLayoutParams<LayoutParams> {
|
|
|
+ width = indicatorWidth?.toInt() ?: 10.dp()
|
|
|
+ height = indicatorHeight.toInt()
|
|
|
+ startToStart = LayoutParams.PARENT_ID
|
|
|
+ endToEnd = LayoutParams.PARENT_ID
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ customViewBinding.vLine.background = indicatorBackground
|
|
|
+ name?.let {
|
|
|
+ customViewBinding.tabTitle.text = name
|
|
|
}
|
|
|
if (isSelected) {
|
|
|
customViewBinding.tabTitle.background = selectedTabBg
|
|
|
@@ -347,15 +443,15 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
selectedTabTextSize
|
|
|
)
|
|
|
if (showIndicator) {
|
|
|
- customViewBinding.vLine.visibility = View.VISIBLE
|
|
|
+ customViewBinding.vLine.visibility = VISIBLE
|
|
|
} else {
|
|
|
- customViewBinding.vLine.visibility = View.GONE
|
|
|
+ customViewBinding.vLine.visibility = GONE
|
|
|
}
|
|
|
} else {
|
|
|
customViewBinding.tabTitle.background = normalTabBg
|
|
|
customViewBinding.tabTitle.setTextColor(normalTabColor)
|
|
|
customViewBinding.tabTitle.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
|
|
|
- customViewBinding.vLine.visibility = View.GONE
|
|
|
+ customViewBinding.vLine.visibility = GONE
|
|
|
customViewBinding.tabTitle.setTextSize(
|
|
|
TypedValue.COMPLEX_UNIT_PX,
|
|
|
normalTabTextSize
|