Skip to content

Instantly share code, notes, and snippets.

@saurabhdhillon
Created February 14, 2019 11:22
Show Gist options
  • Save saurabhdhillon/d982627cb9296d0a2d00f1d664bdb8ac to your computer and use it in GitHub Desktop.
Save saurabhdhillon/d982627cb9296d0a2d00f1d664bdb8ac to your computer and use it in GitHub Desktop.
Distribute items inside recycle-view evenly.
package com.ed.lib.base.utils;
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment