feat(banner): 배너 wrap content 높이를 지원한다
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -74,6 +74,21 @@ class BannerView @JvmOverloads constructor(
|
|||||||
super.onDetachedFromWindow()
|
super.onDetachedFromWindow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
||||||
|
val widthSize = MeasureSpec.getSize(widthMeasureSpec)
|
||||||
|
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
|
||||||
|
val measuredHeightSpec = if (widthSize > 0 && heightMode != MeasureSpec.EXACTLY) {
|
||||||
|
val density = resources.displayMetrics.density
|
||||||
|
val screenWidthDp = (widthSize / density).roundToInt()
|
||||||
|
val desiredHeight = BannerLayoutCalculator.calculate(screenWidthDp, density).itemHeightDp.dpToPx()
|
||||||
|
MeasureSpec.makeMeasureSpec(desiredHeight, MeasureSpec.EXACTLY)
|
||||||
|
} else {
|
||||||
|
heightMeasureSpec
|
||||||
|
}
|
||||||
|
super.onMeasure(widthMeasureSpec, measuredHeightSpec)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
||||||
super.onSizeChanged(w, h, oldw, oldh)
|
super.onSizeChanged(w, h, oldw, oldh)
|
||||||
if (w > 0) applyLayoutSize(w)
|
if (w > 0) applyLayoutSize(w)
|
||||||
|
|||||||
@@ -120,6 +120,36 @@ class BannerViewTest {
|
|||||||
assertEquals(362.dpToPx(), holder.itemView.layoutParams.height)
|
assertEquals(362.dpToPx(), holder.itemView.layoutParams.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `배너 view는 wrap content 높이면 402dp 폭 기준 362dp 높이로 측정된다`() {
|
||||||
|
val view = inflateBannerView()
|
||||||
|
|
||||||
|
view.setItems(listOf(sampleItem("1"), sampleItem("2")))
|
||||||
|
view.measure(exactly(402.dpToPx()), atMost(1_000.dpToPx()))
|
||||||
|
|
||||||
|
assertEquals(362.dpToPx(), view.measuredHeight)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `배너 view는 wrap content 높이면 360dp 폭 기준 320dp 높이로 측정된다`() {
|
||||||
|
val view = inflateBannerView()
|
||||||
|
|
||||||
|
view.setItems(listOf(sampleItem("1"), sampleItem("2")))
|
||||||
|
view.measure(exactly(360.dpToPx()), atMost(1_000.dpToPx()))
|
||||||
|
|
||||||
|
assertEquals(320.dpToPx(), view.measuredHeight)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `배너 view는 명시 높이면 부모 측정 높이를 유지한다`() {
|
||||||
|
val view = inflateBannerView()
|
||||||
|
|
||||||
|
view.setItems(listOf(sampleItem("1"), sampleItem("2")))
|
||||||
|
view.measure(exactly(402.dpToPx()), exactly(500.dpToPx()))
|
||||||
|
|
||||||
|
assertEquals(500.dpToPx(), view.measuredHeight)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `배너 view는 단일 item이면 item 간격을 적용하지 않는다`() {
|
fun `배너 view는 단일 item이면 item 간격을 적용하지 않는다`() {
|
||||||
val view = inflateBannerView()
|
val view = inflateBannerView()
|
||||||
@@ -303,6 +333,8 @@ class BannerViewTest {
|
|||||||
|
|
||||||
private fun exactly(size: Int): Int = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY)
|
private fun exactly(size: Int): Int = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY)
|
||||||
|
|
||||||
|
private fun atMost(size: Int): Int = MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST)
|
||||||
|
|
||||||
private fun Int.dpToPx(): Int {
|
private fun Int.dpToPx(): Int {
|
||||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||||
return (this * context.resources.displayMetrics.density).toInt()
|
return (this * context.resources.displayMetrics.density).toInt()
|
||||||
|
|||||||
Reference in New Issue
Block a user