How do I make WRAP_CONTENT work on a RecyclerView

后端 未结 19 1687
自闭症患者
自闭症患者 2020-11-22 12:49

I have a DialogFragment that contains a RecyclerView (a list of cards).

Within this RecyclerView are one or more CardVi

相关标签:
19条回答
  • 2020-11-22 13:23

    Instead of using any library, easiest solution till the new version comes out is to just open b.android.com/74772. You'll easily find the best solution known to date there.

    PS: b.android.com/74772#c50 worked for me

    0 讨论(0)
  • This comment helped me.Put the recyclerview in any other layout (Relative layout is preferable). Then change recyclerview's height/width as match parent to that layout and set the parent layout's height/width as wrap content.

    0 讨论(0)
  • 2020-11-22 13:27

    I have not worked on my answer but the way I know it StaggridLayoutManager with no. of grid 1 can solve your problem as StaggridLayout will automatically adjust its height and width on the size of the content. if it works dont forget to check it as a right answer.Cheers..

    0 讨论(0)
  • 2020-11-22 13:28

    try deleting : mRecylerView.setHasFixedSize(true);

    0 讨论(0)
  • 2020-11-22 13:29

    You must put a FrameLayout as Main view then put inside a RelativeLayout with ScrollView and at least your RecyclerView, it works for me.

    The real trick here is the RelativeLayout...

    Happy to help.

    0 讨论(0)
  • 2020-11-22 13:32

    UPDATE 02.07.2020
    This method may prevent recycling and should not be used on large data sets.

    UPDATE 05.07.2019

    If you are using RecyclerView inside a ScrollView, just change ScrollView to androidx.core.widget.NestedScrollView. Inside this view there is no need to pack RecyclerView inside a RelativeLayout.

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <!-- other views -->
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <!-- other views -->
    
        </LinearLayout>
    
    </androidx.core.widget.NestedScrollView>
    

    Finally found the solution for this problem.

    All you need to do is wrap the RecyclerView in a RelativeLayout. Maybe there are other Views which may also work.

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题