RecyclerView that doesn't scroll and show all items

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

(I know that it’s stupid to use RecyclerView in this case. I only do this because elsewhere in the application I need a regular RecyclerView with scrolling, etc., but the same content, and I don’t want to duplicate the code).

+30
android android-recyclerview
source share
8 answers

Its pretty simple, just set the height of the RecyclerView s wrap_content .

You might also find it useful to disable nested scrolling in the recycler view, for example:

 RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler); recycler.setNestedScrollingEnabled(false); 
+46
source share

The solution setNestedScrollingEnabled (false) is not as complete as it should: you need to use NestedScrollView instead of ScrollViewfocusableInTouchMode = "true" for the child NestedScrollView.

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

Other alternative solutions that you should consider:

  • Replace ScrollView & RecyclerView with one RecyclerView, which has views with an extra view for what you had in ScrollView

  • Use GridLayout or another format instead.

+23
source share

Perhaps at first glance it is not entirely clear 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.

+8
source share

The easiest way is to use the line in the user adapter inside the onBindViewHolder method: holder.setIsRecyclable(false);

+2
source share

Also try playing with:

 android:overScrollMode 
+1
source share

Below is the code to disable scrolling in the viewport and display all the elements in the layout. This 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:

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

You must replace your scrollView for androidx.core.widget.NestedScrollView with matchparent, it is simple and works fine.

0
source share

I realized that I was using a BottomNavigationView which blocked my processor view from displaying the last item. I fixed this by adding a 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" /> 
0
source share

All Articles