0

Trying to get pixel height of adapter list item before any items are adapted

var item =
    LayoutInflater.from(context).inflate(R.layout.adapter_item, null)

item.post {
    System.out.println("height=$item.height")
}

Above ^^^ I inflate an item in the fragment that references and uses the adapter. Then I print the height as a runnable so I know height is not called too early and will be non zero.

Problem is runnable does not run, and item height is not printed.

What is my issue? Thanks

relevant post View's getWidth() and getHeight() returns 0

edit

I've also tried this, but there is no print statement

    item.viewTreeObserver.addOnGlobalLayoutListener {
        //At this point the layout is complete and the
        //dimensions of myView and any child views are known.
        System.out.println("height=$item.height")
    }

I think possibly the issue is related to inflating a layout which is not in a visible hierarchy?

the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

0

Looks like I just have to determine the height from the adapter. I can see the print out.

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    
    var item = LayoutInflater.from(parent.context)
        .inflate(R.layout.list_item, parent, false) as ConstraintLayout

    item.post {
        System.out.println("height=${item.height}")
    }

    return MyViewHolder(
        item
    )
}
the_prole
  • 8,275
  • 16
  • 78
  • 163
0

You can try

var item = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false) as ConstraintLayout
var params = item.layoutParams
Log.e("height->",params.height.toString())
Mohammed Alaa
  • 3,140
  • 2
  • 19
  • 21