简体   繁体   中英

Android: last card of RecyclerView cut off. Can't scroll any further. Why would this happen?

Here is a screenshot of what is happening:

在此处输入图像描述

For some reason, the last card of the RecyclerView is not showing up properly. This is a weird occurrence especially since the RecyclerView is just wrapping content.

<android.support.v7.widget.RecyclerView
    android:id="@+id/cardList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    android:layout_marginLeft="8dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Why might something like this occur? How can I make it so that the RecyclerView is not cut off?

Solved by poss: https://stackoverflow.com/users/4048794/poss

choosing wrap_content as the attribute for layout_width and layout_height causes the cut-off on RecyclerViews

RecyclerView cannot have WRAP_CONTENT as layout_height. The solution is to set layout_height to a specific value like "100dp", like the following:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    ...
/>

Now if the value in the xml is not what you want, you can set layout_height in the code using requestLayout();

I have a RecyclerView similar to Marcin Orlowski's. Now it works for me after I applied the solution of setting the height of layout_height in Java using requestLayout. It cannot be set in onCreate method, however. It needs to be set in onWindowFocusChanged method.

@Override
public void onWindowFocusChanged (boolean hasFocus) {
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int[] loc = new int[2];
    recyclerView.getLocationOnScreen(loc); // get the location of the recyclerview
    int distance_to_bottom = dm.heightPixels - loc[1]; // similar to Marcin's design
    // RecyclerView 直到版面的最下端
    ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
    params.height = distance_to_bottom;
    recyclerView.requestLayout(); // set it right here
}

I had the same problem.Then i try to add some space in the bottom of the recyclerview. If you try all the above solutions and still got the issue, then try this one.

add

android:clipToPadding="false"

to your recyclerview code . like this

<RecyclerView
 android:id="@+id/my_rv"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="10dp"
 android:clipToPadding="false" />

Same happened to me, so I just added three main inside my recycler View:

  1. First of all, give your recycler View height and width as: match parent

  2. Secondly give bottom padding, as paddingBottom = "10dp"

  3. Include, clipToPadding = "false"

    <androidx.recyclerview.widget.RecyclerView android:id="@+id/postRV" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="10dp" android:clipToPadding="false" android:layout_marginTop="80dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM