Gridview height gets cut

后端 未结 6 1537
刺人心
刺人心 2020-11-22 17:26

I\'m trying to display 8 items inside a gridview. Sadly, the gridview height is always too little, so that it only shows the first row, and a little part of the second.

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 17:42

    Jacob R solution in Kotlin:

    class ExpandableHeightGridView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
    ) : GridView(context, attrs, defStyleAttr) {
    
        override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
            val expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST)
            super.onMeasure(widthMeasureSpec, expandSpec)
            layoutParams.height = measuredHeight
        }
    }
    

    After adding GridView to RecyclerView I got a full-size GridView (all rows are visible), as expected.

提交回复
热议问题