Skip to content

Commit

Permalink
+ child text/image view attr
Browse files Browse the repository at this point in the history
  • Loading branch information
angcyo committed Jul 7, 2023
1 parent 9ce915a commit cfbb090
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
36 changes: 36 additions & 0 deletions TabLayout/src/main/java/com/angcyo/tablayout/DslTabLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ open class DslTabLayout(
Gravity.CENTER_VERTICAL -> measuredHeight - paddingBottom -
((measuredHeight - paddingTop - paddingBottom - _maxConvexHeight) / 2 -
childView.measuredHeight / 2)

Gravity.BOTTOM -> measuredHeight - paddingBottom
else -> paddingTop + lp.topMargin + childView.measuredHeight
}
Expand Down Expand Up @@ -1331,6 +1332,7 @@ open class DslTabLayout(
childLeft = when (horizontalGravity) {
Gravity.CENTER_HORIZONTAL -> paddingStart + ((measuredWidth - paddingStart - paddingEnd - _maxConvexHeight) / 2 -
childView.measuredWidth / 2)

Gravity.RIGHT -> measuredWidth - paddingRight - childView.measuredWidth - lp.rightMargin
else -> paddingLeft + lp.leftMargin
}
Expand Down Expand Up @@ -1388,6 +1390,18 @@ open class DslTabLayout(
var indicatorContentIndex = -1
var indicatorContentId = View.NO_ID

/**[com.angcyo.tablayout.DslTabLayoutConfig.getOnGetTextStyleView]*/
var contentTextViewIndex = -1

/**[com.angcyo.tablayout.DslTabLayoutConfig.getTabTextViewId]*/
var contentTextViewId = View.NO_ID

/**[com.angcyo.tablayout.DslTabLayoutConfig.getOnGetIcoStyleView]*/
var contentIconViewIndex = -1

/**[com.angcyo.tablayout.DslTabLayoutConfig.getTabIconViewId]*/
var contentIconViewId = View.NO_ID

/**
* 剩余空间占比, 1f表示占满剩余空间, 0.5f表示使用剩余空间的0.5倍
* [android.widget.LinearLayout.LayoutParams.weight]*/
Expand Down Expand Up @@ -1417,6 +1431,24 @@ open class DslTabLayout(
weight = a.getFloat(R.styleable.DslTabLayout_Layout_layout_tab_weight, weight)
highlightDrawable =
a.getDrawable(R.styleable.DslTabLayout_Layout_layout_highlight_drawable)

contentTextViewIndex = a.getInt(
R.styleable.DslTabLayout_Layout_layout_tab_text_view_index,
contentTextViewIndex
)
contentIconViewIndex = a.getInt(
R.styleable.DslTabLayout_Layout_layout_tab_text_view_index,
contentIconViewIndex
)
contentTextViewId = a.getResourceId(
R.styleable.DslTabLayout_Layout_layout_tab_text_view_id,
contentTextViewId
)
contentIconViewId = a.getResourceId(
R.styleable.DslTabLayout_Layout_layout_tab_icon_view_id,
contentIconViewIndex
)

a.recycle()

if (gravity == UNSPECIFIED_GRAVITY) {
Expand Down Expand Up @@ -1779,13 +1811,15 @@ open class DslTabLayout(
val viewCenterX = measuredWidth / 2
childTargetX - viewCenterX - scrollX
}

isLayoutRtl -> {
if (childTargetX < viewDrawTargetX) {
childTargetX - viewDrawTargetX - scrollX
} else {
-scrollX
}
}

else -> {
if (childTargetX > viewDrawTargetX) {
childTargetX - viewDrawTargetX - scrollX
Expand All @@ -1803,9 +1837,11 @@ open class DslTabLayout(
val viewCenterY = measuredHeight / 2
childTargetY - viewCenterY - scrollY
}

childTargetY > viewDrawTargetY -> {
childTargetY - viewDrawTargetY - scrollY
}

else -> {
if (tabIndicator.indicatorGravity == DslTabIndicator.INDICATOR_GRAVITY_END &&
childTargetY < viewDrawTargetY
Expand Down
26 changes: 26 additions & 0 deletions TabLayout/src/main/java/com/angcyo/tablayout/DslTabLayoutConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ open class DslTabLayoutConfig(val tabLayout: DslTabLayout) : DslSelectorConfig()
}
}
}

if (lp.contentTextViewIndex != -1 && itemView is ViewGroup) {
itemView.getChildOrNull(lp.contentTextViewIndex)?.let {
if (it is TextView) {
tv = it
}
}
}

if (lp.contentTextViewId != View.NO_ID) {
itemView.findViewById<View>(lp.contentTextViewId)?.let {
if (it is TextView) {
tv = it
}
}
}
}
tv
} else {
Expand Down Expand Up @@ -177,6 +193,16 @@ open class DslTabLayoutConfig(val tabLayout: DslTabLayout) : DslSelectorConfig()
iv = it
}
}

if (lp.contentIconViewIndex != -1 && itemView is ViewGroup) {
iv = itemView.getChildOrNull(lp.contentIconViewIndex)
}

if (lp.contentIconViewId != View.NO_ID) {
itemView.findViewById<View>(lp.contentIconViewId)?.let {
iv = it
}
}
}
iv
} else {
Expand Down
5 changes: 5 additions & 0 deletions TabLayout/src/main/res/values/attr_dsl_tab_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,10 @@
<attr name="layout_tab_weight" format="float" />
<!--单独配置的drawable, 可以覆盖tab中的配置-->
<attr name="layout_highlight_drawable" format="reference" />

<attr name="layout_tab_text_view_index" format="integer" />
<attr name="layout_tab_icon_view_index" format="integer" />
<attr name="layout_tab_text_view_id" format="reference" />
<attr name="layout_tab_icon_view_id" format="reference" />
</declare-styleable>
</resources>

0 comments on commit cfbb090

Please sign in to comment.