【问题标题】:How to know pixel height of adapter items before items adapted如何在项目适应之前知道适配器项目的像素高度
【发布时间】:2020-08-05 17:22:07
【问题描述】:

在适配任何项目之前尝试获取适配器列表项目的像素高度

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

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

上面 ^^^ 我对片段中引用和使用适配器的项目进行膨胀。然后我将高度打印为可运行对象,因此我知道高度不会过早调用并且不会为零。

Problem is runnable 不运行,item height 不打印。

我的问题是什么?谢谢

相关帖子 View's getWidth() and getHeight() returns 0

编辑

这个我也试过了,但是没有打印语句

    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")
    }

我认为问题可能与膨胀不在可见层次结构中的布局有关?

【问题讨论】:

    标签: android android-layout android-fragments android-view


    【解决方案1】:

    看起来我只需要确定适配器的高度。我可以看到打印出来的。

    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
        )
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试

      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())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-24
        • 1970-01-01
        • 1970-01-01
        • 2018-11-25
        • 2011-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多