Large gap forms between RecyclerView items when scrolling down

前端 未结 9 1854
别跟我提以往
别跟我提以往 2020-12-23 20:16

I\'m making a ToDo list app, and while testing it, for some reason, a huge gap forms between the items whenever I try to scroll down. It always happens whenever I Drag and D

相关标签:
9条回答
  • 2020-12-23 20:43

    Just for the record: Since in my case we had to implement some "superfancy UI" with overlapping RecyclerView and blur effect toolbar, I had to get rid of clipToPadding="false" within RecyclerView-xml.

    <android.support.v7.widget.RecyclerView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="@dimen/height_toolbar"
      android:paddingBottom="@dimen/height_bottom_bar_container"
      //android:clipToPadding="false" <-- remove this flag!
      android:scrollbars="vertical"
    />
    

    paddingTopand paddingBottom worked, but we replaced it with some GapViews(empty views, with height of paddingTop and paddingBottom)

    0 讨论(0)
  • 2020-12-23 20:43

    You can try setting your Viewgroup holding the recyclerview to wrap content

    0 讨论(0)
  • 2020-12-23 20:44

    i have this problem and i just use

    android:layout_height="wrap_content"
    

    for parent of every item.

    0 讨论(0)
  • 2020-12-23 20:53

    Luksprog's answer to Gabriele Mariotti's answer works.

    According to the doc

    With the release 2.3.0 there is an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. >This means that previously unavailable scenarios, such as using WRAP_CONTENT >for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

    Due to this change, make sure to double check the layout parameters of your item views: previously ignored layout parameters (such as MATCH_PARENT in the scroll direction) will now be fully respected.

    In your item layout you have to change:

    android:layout_height="match_parent"
    

    with

    android:layout_height="wrap_content" 
    
    0 讨论(0)
  • 2020-12-23 20:55

    change in Recycler view match_parent to wrap_content:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    Also change in item layout xml

    Make parent layout height match_parent to wrap_content

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />
    

    0 讨论(0)
  • 2020-12-23 20:56

    Avoid taking the view in item layout with a container like this:

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
    <data/>
    
    <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?android:listPreferredItemHeight"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </android.support.constraint.ConstraintLayout>
    </layout>
    

    as in this case match_parent will do its work and the problem will still remain! Rather take it like this:

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
        <data/>
    
        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?android:listPreferredItemHeight"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </layout>
    

    [Note: Above code has data binding attached to it, don't use <layout> & <data> tags if not using data binding]

    Other than this, if you must use any view group containers, than take a look the height and width attributes in the container, and try change those from match_parent to wrap_content. That should resolve the issue. For more transparency, one can try giving background colours and see through it to identify actual problem.

    0 讨论(0)
提交回复
热议问题