How to know when the RecyclerView has finished laying down the items?

后端 未结 12 1474
野的像风
野的像风 2020-11-29 20:06

I have a RecyclerView that is inside a CardView. The CardView has a height of 500dp, but I want to shorten this height if the Re

12条回答
  •  情书的邮戳
    2020-11-29 20:59

    If you are using the android-ktx library and if you need to perform an action after positioning all elements of the Activity, you can use this method:

    // define 'afterMeasured' Activity listener:
    fun Activity.afterMeasured(f: () -> Unit) {
        window.decorView.findViewById(android.R.id.content).doOnNextLayout {
            f()
        }
    }
    
    // in Activity:
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(...)
    
        afterMeasured {
            // do something here
        }    
    }
    

提交回复
热议问题