Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LinearLayoutManager implementation that stretches to fit all list items on screen and disables scrolling. Useful for dashboards etc.
MIT License
Copyright (c) 2020 Jan Heinrich Reimer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
package de.wilhelmgym.quiz.recyclerview;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class SpanningLinearLayoutManager extends LinearLayoutManager {
public SpanningLinearLayoutManager(Context context) {
super(context);
}
public SpanningLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public SpanningLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return spanLayoutSize(super.generateDefaultLayoutParams());
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
return spanLayoutSize(super.generateLayoutParams(c, attrs));
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
return spanLayoutSize(super.generateLayoutParams(lp));
}
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
return super.checkLayoutParams(lp);
}
private RecyclerView.LayoutParams spanLayoutSize(RecyclerView.LayoutParams layoutParams){
if(getOrientation() == HORIZONTAL){
layoutParams.width = (int) Math.round(getHorizontalSpace() / (double) getItemCount());
}
else if(getOrientation() == VERTICAL){
layoutParams.height = (int) Math.round(getVerticalSpace() / (double) getItemCount());
}
return layoutParams;
}
@Override
public boolean canScrollVertically() {
return false;
}
@Override
public boolean canScrollHorizontally() {
return false;
}
private int getHorizontalSpace() {
return getWidth() - getPaddingRight() - getPaddingLeft();
}
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
}
@mhd-adeeb-masoud

This comment has been minimized.

Copy link

@mhd-adeeb-masoud mhd-adeeb-masoud commented Feb 1, 2017

Thank you this has been helpful 👍

@sudansh

This comment has been minimized.

Copy link

@sudansh sudansh commented Mar 23, 2017

This works only if the width is match_parent.
Any solution if width is wrap_content?

@heinrichreimer

This comment has been minimized.

Copy link
Owner Author

@heinrichreimer heinrichreimer commented Mar 30, 2017

@sudansh No, wrap_content wouldn't work as this Layout manager explicitly tries to distribute the available space to all cells.
wrap_content would make no sense here, as it would make the layout manager behave like the normal LinearLayoutManager.

@kostyabakay

This comment has been minimized.

Copy link

@kostyabakay kostyabakay commented May 19, 2017

Thanks!

@jtrollkarl

This comment has been minimized.

Copy link

@jtrollkarl jtrollkarl commented Jul 3, 2017

Is there a way for this to not stretch my views inside the manager? I'd like to keep the width/height of my views, because as of right now, the width is being stretched yet i explicity set it to 50dp

@oiyio

This comment has been minimized.

Copy link

@oiyio oiyio commented Feb 26, 2018

it works like a charm! thanks.

@boyan01

This comment has been minimized.

Copy link

@boyan01 boyan01 commented Apr 10, 2018

nice job

@mehulTank

This comment has been minimized.

Copy link

@mehulTank mehulTank commented Apr 20, 2018

good job it is very help full to me

@ismdcf

This comment has been minimized.

Copy link

@ismdcf ismdcf commented Sep 6, 2018

Thank you saved my day had to alter a bit to show two rows and 3 columns on a gridadapter

@Bomikuu

This comment has been minimized.

Copy link

@Bomikuu Bomikuu commented Nov 6, 2018

Works like a charm, you just saved my life. Cheers!

@afzafri

This comment has been minimized.

Copy link

@afzafri afzafri commented Nov 11, 2018

Just what I need for my project. Thank you, you just saved my life!

@Miha-x64

This comment has been minimized.

Copy link

@Miha-x64 Miha-x64 commented Feb 18, 2019

Thank you! I've changed this a bit in order to allow adding and removing items.

@SAGARSURI

This comment has been minimized.

Copy link

@SAGARSURI SAGARSURI commented Jun 18, 2019

@Miha-x64 your version is amazing!

@heinrichreimer

This comment has been minimized.

Copy link
Owner Author

@heinrichreimer heinrichreimer commented Jun 19, 2019

@jtrollkarl as I said in my previous comment, items with self-determined width/height would make no sense in conjunction with this layout manager. Use a standard LinearLayoutManager instead.

@SAGARSURI

This comment has been minimized.

Copy link

@SAGARSURI SAGARSURI commented Jun 19, 2019

@heinrichreimer how can I make this scrollable. I have around 10 items. Currently they are really close to each other.

@heinrichreimer

This comment has been minimized.

Copy link
Owner Author

@heinrichreimer heinrichreimer commented Jun 21, 2019

@SAGARSURI You shouldn't use this or @miha-x86's layout manager for scrollable lists. It is intended only for lists that should fit on one page, as stated in the Gist description.

@rajeshdeva23

This comment has been minimized.

Copy link

@rajeshdeva23 rajeshdeva23 commented Aug 20, 2019

Horizontal recycler view is not scrolling

@heinrichreimer

This comment has been minimized.

Copy link
Owner Author

@heinrichreimer heinrichreimer commented Aug 21, 2019

@rajeshdeva23 You shouldn't use this layout manager for scrollable lists. It is intended only for lists that should fit on one page, as stated in the Gist description.

@adnanhafeez04

This comment has been minimized.

Copy link

@adnanhafeez04 adnanhafeez04 commented May 5, 2020

This saved my life ... Thanks you so much.

@mianaliasjad

This comment has been minimized.

Copy link

@mianaliasjad mianaliasjad commented Nov 14, 2020

Hi this is great!

But I want to use it only when my all items width is less than the screen width. Otherwise, I want my items to be scrollable.

Is there a way to achieve this?

@heinrichreimer

This comment has been minimized.

Copy link
Owner Author

@heinrichreimer heinrichreimer commented Nov 16, 2020

@mianaliasjad I would just change the layout manager based on the screen width. That should be easier than to write two layout managers at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment