make RecyclerView's height to “wrap_content” in Constraint layout

前端 未结 3 616
时光说笑
时光说笑 2020-12-25 12:03

i\'m trying to set the height of recycler view on wrap_content and make it respect that,but it will exceed the other widget on layout. what can i do now?

<         


        
3条回答
  •  时光说笑
    2020-12-25 12:20

    I had this issue. I needed to build a UI page that contained a RecyclerView with an unknown number of elements that should scroll. The page had the the following layout:

    LinearLayout
        ConstraintLayout
            TextView
            RecyclerView
            TextView
    

    and I wanted heights to be determined procedurally for everything using match_parent and wrap_content.

    I wanted the RecyclerView to use all the space available between the two TextViews.

    The key was to give the RecyclerView an "impossible" constraint, by doing this:

    app:layout_constraintTop_toBottomOf="@+id/topTextView"
    app:layout_constraintBottom_toTopOf="@+id/bottomTextView"
    

    and adding this:

    app:layout_constrainedHeight="true"
    

    If you need to add margin between the top TextView and RecyclerView (and/or the bottom TextView and RecyclerView) put the margin on the RecyclerView, not the TextViews.

    Code Examples

    Below is the actual code I'm using (with some text styling removed for readability)

    
    
    
        
    
            
    
            
    
            
    
        
    
    

    As a final note, the HolderView also uses only wrap_content. Here's that code as well:

    
    
    
        
    
    

提交回复
热议问题