Skip to content

Commit

Permalink
[android] 修复GridItem高度计算
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Aug 7, 2024
1 parent 21713d8 commit db4e2ed
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1239,10 +1239,7 @@ class GXComponentGridTest : GXBaseTest() {
Assert.assertEquals(1080F.dpToPx(), rootView.width())
Assert.assertEquals(200F.dpToPx(), rootView.height())
Assert.assertEquals(true, (rootView as? RecyclerView)?.layoutManager?.canScrollVertically())
Assert.assertEquals(
false,
(rootView as? RecyclerView)?.layoutManager?.canScrollHorizontally()
)
Assert.assertEquals(false, (rootView as? RecyclerView)?.layoutManager?.canScrollHorizontally())
}

@Test
Expand Down Expand Up @@ -1280,14 +1277,8 @@ class GXComponentGridTest : GXBaseTest() {
Assert.assertEquals(1080F.dpToPx(), rootView.child(1).width())
Assert.assertEquals(100F.dpToPx(), rootView.child(1).height())

Assert.assertEquals(
true,
(rootView.child(1) as? RecyclerView)?.layoutManager?.canScrollVertically()
)
Assert.assertEquals(
false,
(rootView.child(1) as? RecyclerView)?.layoutManager?.canScrollHorizontally()
)
Assert.assertEquals(true, (rootView.child(1) as? RecyclerView)?.layoutManager?.canScrollVertically())
Assert.assertEquals(false, (rootView.child(1) as? RecyclerView)?.layoutManager?.canScrollHorizontally())
}

@Test
Expand Down Expand Up @@ -1316,10 +1307,7 @@ class GXComponentGridTest : GXBaseTest() {

rootView.executeRecyclerView()

Assert.assertEquals(
Color.parseColor("#e4e4e4"),
(rootView?.background as? GradientDrawable)?.colors?.get(0)
)
Assert.assertEquals(Color.parseColor("#e4e4e4"), (rootView?.background as? GradientDrawable)?.colors?.get(0))
}

@Test
Expand Down Expand Up @@ -1427,5 +1415,8 @@ class GXComponentGridTest : GXBaseTest() {

Assert.assertEquals(1080F.dpToPx(), rootView.width())
Assert.assertEquals(100F.dpToPx() * 2 + 200F.dpToPx(), rootView.height())
Assert.assertEquals(100F.dpToPx(), rootView.child(0).height())
Assert.assertEquals(200F.dpToPx(), rootView.child(1).height())
Assert.assertEquals(100F.dpToPx(), rootView.child(2).height())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,7 @@ class GXTemplateEngine {
): GXTemplateContext {
val gxTemplateInfo = data.getTemplateInfo(gxTemplateItem)

val gxTemplateContext = GXTemplateContext.createContext(
gxTemplateItem, gxMeasureSize, gxTemplateInfo, gxExtendParams?.gxVisualTemplateNode
)
val gxTemplateContext = GXTemplateContext.createContext(gxTemplateItem, gxMeasureSize, gxTemplateInfo, gxExtendParams?.gxVisualTemplateNode)

val gxHostTemplateContext = gxExtendParams?.gxHostTemplateContext
if (gxHostTemplateContext != null) {
Expand All @@ -712,9 +710,7 @@ class GXTemplateEngine {
* @suppress
* @hide
*/
fun createViewOnlyViewTree(
gxTemplateContext: GXTemplateContext
): View? {
fun createViewOnlyViewTree(gxTemplateContext: GXTemplateContext): View? {
if (Log.isLog()) {
Log.e("createViewOnlyViewTree")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,22 @@ object GXNodeUtils {
val itemTemplateItem = itemTemplatePair.first
val itemVisualTemplateNode = itemTemplatePair.second

if (gxTemplateContext.gridItemLayoutCache == null) {
gxTemplateContext.gridItemLayoutCache = computeGridItemLayout(
val assumptionItemsSameHeight = gxNode.templateNode.getExtend()?.getBoolean(GXTemplateKey.GAIAX_GRID_EXTEND_ITEM_SAME_HEIGHT) ?: true

if (assumptionItemsSameHeight) {
if (gxTemplateContext.gridItemLayoutCache == null) {
gxTemplateContext.gridItemLayoutCache = computeGridItemLayout(
gxTemplateContext,
itemViewPort,
itemTemplateItem,
itemVisualTemplateNode,
itemData,
itemCacheKey
)
}
return gxTemplateContext.gridItemLayoutCache
} else {
return computeGridItemLayout(
gxTemplateContext,
itemViewPort,
itemTemplateItem,
Expand All @@ -240,7 +254,6 @@ object GXNodeUtils {
itemCacheKey
)
}
return gxTemplateContext.gridItemLayoutCache
}

private fun computeScrollItemContainerSize(
Expand Down Expand Up @@ -531,9 +544,8 @@ object GXNodeUtils {
gxItemTemplateItem,
itemMeasureSize,
itemTemplateData,
gxItemVisualTemplateNode,
itemCacheKey
)
gxItemVisualTemplateNode
)?.layoutByBind
}

private fun computeSliderItemLayout(
Expand All @@ -551,9 +563,8 @@ object GXNodeUtils {
gxItemTemplateItem,
gxMeasureSize,
gxTemplateData,
gxItemVisualTemplateNode,
itemCacheKey
)
gxItemVisualTemplateNode
)?.layoutByBind
}

private fun computeScrollItemLayout(
Expand All @@ -572,8 +583,10 @@ object GXNodeUtils {
gxMeasureSize,
gxTemplateData,
gxItemVisualTemplateNode,
itemCacheKey
)
)?.apply {
gxTemplateContext.initNodeForScroll()
gxTemplateContext.putNodeForScroll(itemCacheKey, this)
}?.layoutByBind
}

private fun computeGridItemLayout(
Expand All @@ -591,9 +604,8 @@ object GXNodeUtils {
gxItemTemplateItem,
gxMeasureSize,
gxTemplateData,
gxItemVisualTemplateNode,
itemCacheKey
)
gxItemVisualTemplateNode
)?.layoutByBind
}

fun computeScrollAndGridFooterItemViewPort(
Expand Down Expand Up @@ -758,15 +770,12 @@ object GXNodeUtils {
gxTemplateItem: GXTemplateEngine.GXTemplateItem,
gxMeasureSize: GXTemplateEngine.GXMeasureSize,
gxTemplateData: GXTemplateEngine.GXTemplateData,
gxVisualTemplateNode: GXTemplateNode?,
itemCacheKey: String
): Layout? {
gxVisualTemplateNode: GXTemplateNode?
): GXNode? {

val gxItemTemplateInfo = GXTemplateEngine.instance.data.getTemplateInfo(gxTemplateItem)

val gxItemTemplateContext = GXTemplateContext.createContext(
gxTemplateItem, gxMeasureSize, gxItemTemplateInfo, gxVisualTemplateNode
)
val gxItemTemplateContext = GXTemplateContext.createContext(gxTemplateItem, gxMeasureSize, gxItemTemplateInfo, gxVisualTemplateNode)

if (!GXGlobalCache.instance.isExistForPrepareView(gxMeasureSize, gxTemplateItem)) {
GXTemplateEngine.instance.render.prepareView(gxItemTemplateContext)
Expand All @@ -778,10 +787,7 @@ object GXNodeUtils {

GXTemplateEngine.instance.render.bindViewDataOnlyNodeTree(gxItemTemplateContext)

gxTemplateContext.initNodeForScroll()
gxTemplateContext.putNodeForScroll(itemCacheKey, gxItemRootNode)

return gxItemRootNode.layoutByBind
return gxItemRootNode
}

private fun computeScrollContainerSize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ data class GXTemplateNode(
return dataCache
}

fun getExtend(templateData: JSON?): JSONObject? {
fun getExtend(templateData: JSON? = null): JSONObject? {
if (dataExtendCache == null) {
dataExtendCache = dataBinding?.getExtend(templateData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ class GXContainerViewAdapter(val gxTemplateContext: GXTemplateContext, private v
Log.e("bindGXViewHolder $holder ${holder.itemView} ")
}

val templateItem =
holder.templateItem ?: throw IllegalArgumentException("templateItem is null")
val templateItem = holder.templateItem ?: throw IllegalArgumentException("templateItem is null")

val isFooterItem = templateItem == footerTemplateItem

Expand All @@ -178,9 +177,7 @@ class GXContainerViewAdapter(val gxTemplateContext: GXTemplateContext, private v

val itemMeasureSize = getMeasureSize(itemViewPort)

val itemContainerSize = getItemContainerSize(
isFooterItem, templateItem, visualNestTemplateNode, itemViewPort
)
val itemContainerSize = getItemContainerSize(isFooterItem, templateItem, visualNestTemplateNode, itemViewPort)

val itemContainerLayoutParams = getItemContainerSize(itemContainerSize)

Expand Down Expand Up @@ -370,9 +367,7 @@ class GXContainerViewAdapter(val gxTemplateContext: GXTemplateContext, private v
)

private fun getItemViewPort(isFooterItem: Boolean) =
if (isFooterItem) GXNodeUtils.computeScrollAndGridFooterItemViewPort(
gxTemplateContext, gxNode
)
if (isFooterItem) GXNodeUtils.computeScrollAndGridFooterItemViewPort(gxTemplateContext, gxNode)
else GXNodeUtils.computeScrollAndGridItemViewPort(gxTemplateContext, gxNode)

override fun getItemViewType(position: Int): Int {
Expand Down

0 comments on commit db4e2ed

Please sign in to comment.