RecyclerView that does not scroll and shows all items

AndroidAndroid Recyclerview

Android Problem Overview


I have a RecyclerView (and some other views) in a ScrollView. Currently the RecyclerView is laid out as very small (it shows 2 items out of 5 that it contains) and it scrolls independently of the ScrollView, which is obviously not great UX. I would like to get the RecyclerView to not scroll and to extend so that all its items are visible.

(I know it's stupid to use a RecyclerView in this case. I'm only doing this because somewhere else in the app I need a normal RecyclerView with scrolling etc. but the same kind of content, and I don't want to duplicate code).

Android Solutions


Solution 1 - Android

It’s pretty simple, simply set the RecyclerView’s height to wrap_content.

You might also benefit from disabling nested scrolling on the recycler view, like so:

RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setNestedScrollingEnabled(false);

Solution 2 - Android

The solution of setNestedScrollingEnabled(false) isn't as full as it should: you need to use NestedScrollView instead of ScrollViewfocusableInTouchMode="true" to the child of the NestedScrollView .

If you insist on using ScrollView, you should also set minHeight to the RecyclerView, and also set overScrollMode="never" . In this case, it still isn't a good solution because the minHeight might not be enough in some cases

Other alternative solutions that you should consider:

  1. Replace the ScrollView&RecyclerView with a single RecyclerView, which has views with additional view type for what you had in the ScrollView

  2. Use GridLayout or another layout instead.

Solution 3 - Android

Maybe it is not completely clear at first sight what to do with all these answers. I just tried them and the working one is:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/person_properties"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
...
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:overScrollMode="never" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

No need to change anything programmatically.

Solution 4 - Android

In your activity.xml file

<androidx.core.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityName">
        
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="false">
        
     </androidx.recyclerview.widget.RecyclerView>
        
</androidx.core.widget.NestedScrollView>

In RecyclerView use android:nestedSrollingEnabled="false" and use NestedScrollView as a parent Scroll View.

Solution 5 - Android

If you are using RecyclerView inside ScrollView then Replace ScrollView with NestedScrollView and enable the nested scrolling

android:nestedScrollingEnabled="false"

This Solved my problem

Solution 6 - Android

An easy way is to use in your Custom Adapter, inside the onBindViewHolder method this line: holder.setIsRecyclable(false);

Solution 7 - Android

I realised that I use BottomNavigationView which blocked my recycler view from displaying the last item. I fixed it by adding paddingBottom to it:

    <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recipient_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="70dp"
                app:layout_constraintTop_toBottomOf="@id/view"
                />

Solution 8 - Android

Also try to play with:

android:overScrollMode

Solution 9 - Android

You should replace your scrollView for androidx.core.widget.NestedScrollView with matchparent, its simple and work fine.

Solution 10 - Android

Following is the code for disabling scroll in the recycler-view, and showing all the items in your layout. It might work:

public class NoScrollRecycler extends RecyclerView {

    public NoScrollRecycler(Context context){
        super(context);
    }

    public NoScrollRecycler(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    public NoScrollRecycler(Context context, AttributeSet attrs, int style){
        super(context, attrs, style);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){

        //Ignore scroll events.
        if(ev.getAction() == MotionEvent.ACTION_MOVE)
            return true;

        //Dispatch event for non-scroll actions, namely clicks!
        return super.dispatchTouchEvent(ev);
    }
}

use like this way:

<com.example.custom.NoScrollRecycler
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_white"/>

Solution 11 - Android

probably parent of recyclerView is a constraintLayout changed it to RelativeLayout

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpstobieckiView Question on Stackoverflow
Solution 1 - AndroidnatarioView Answer on Stackoverflow
Solution 2 - Androidandroid developerView Answer on Stackoverflow
Solution 3 - AndroidValentina KonyukhovaView Answer on Stackoverflow
Solution 4 - AndroidAmit SinghView Answer on Stackoverflow
Solution 5 - Androidsuhas sasukeView Answer on Stackoverflow
Solution 6 - AndroidGuillermo GonzalezView Answer on Stackoverflow
Solution 7 - AndroidAndrii ArtamonovView Answer on Stackoverflow
Solution 8 - AndroidAtetcView Answer on Stackoverflow
Solution 9 - AndroidPabelView Answer on Stackoverflow
Solution 10 - AndroidPankaj TalaviyaView Answer on Stackoverflow
Solution 11 - AndroidMajid ZareView Answer on Stackoverflow