Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView that does not scroll and shows all items

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).

like image 237
pstobiecki Avatar asked Jul 04 '16 22:07

pstobiecki


People also ask

How can show all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.

Is recycler view scrollable?

To help you build apps with lists, Android provides the RecyclerView . RecyclerView is designed to be very efficient, even with large lists, by reusing, or recycling, the views that have scrolled off the screen.

What is LayoutManager in RecyclerView?

This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

What is ItemDecoration RecyclerView?

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more.


2 Answers

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); 
like image 150
natario Avatar answered Sep 23 '22 11:09

natario


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.

like image 45
android developer Avatar answered Sep 20 '22 11:09

android developer