I donβt like the answer chosen, so let me provide mine: instead of wrapping the entire layout of the element in SomeDammyLayoutWithFixedAspectRatio, you can hack the GridLayoutManager and rewrite the code inside measureChild. I replaced these lines:
if (mOrientation == VERTICAL) { wSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, horizontalInsets, lp.width, false); hSpec = getChildMeasureSpec(mOrientationHelper.getTotalSpace(), getHeightMode(), verticalInsets, lp.height, true); } else { hSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, verticalInsets, lp.height, false); wSpec = getChildMeasureSpec(mOrientationHelper.getTotalSpace(), getWidthMode(), horizontalInsets, lp.width, true); }
to:
if (mOrientation == VERTICAL) { wSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, horizontalInsets, lp.width, false); hSpec = wSpec; } else { hSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, verticalInsets, lp.height, false); wSpec = hSpec; }
Everything seems to be fine.
Don't get me wrong, this is also pretty dirty, but at least this solution will not hurt application performance by expanding the view hierarchy
Karma Maker Jul 05 '17 at 19:01 2017-07-05 19:01
source share