How to set RecyclerView Max Height

前端 未结 12 1651
陌清茗
陌清茗 2020-12-01 15:55

I want to set Max Height of RecylerView.I am able to set max height using below code.Below code makes height 60% of current screen.

     DisplayMetrics displ         


        
12条回答
  •  无人及你
    2020-12-01 16:09

    I just had this same problem, and wanted to share a new, up-to-date answer. This one works on at least Android 9 and Android Studio v. 4.0.

    Wrap the RecyclerView in a ConstraintLayout, as some of the answers here have suggested. But it does not appear you can set the maximum height of that layout in XML. Instead, you must set it in code, as follows: Give the ConstraintLayout an ID, and then, in your onCreate or onViewCreated method for the activity or fragment, respectively, in question - for example, to set a max height of 200 dp:

    findViewById(R.id.[YOUR ID]).maxHeight = 200 // activity
    view.findViewById(R.id.[YOUR ID]).maxHeight = 200 // fragment
    

    Annoyingly, ConstraintLayout does expose an XML attribute android:minHeight but no android:maxHeight.

    Even more annoyingly, RecyclerView itself apparently comes with an XML attribute named android:maxHeight, but that attribute does not work.

提交回复
热议问题