Skip to content

Instantly share code, notes, and snippets.

@hanihashemi
Last active September 8, 2015 10:24
Show Gist options
  • Save hanihashemi/1bbd8757fe4bfa19d9ab to your computer and use it in GitHub Desktop.
Save hanihashemi/1bbd8757fe4bfa19d9ab to your computer and use it in GitHub Desktop.
Android HeightResizableLinearLayoutManager
package com.oostaa.app.widgets;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by Hani on 9/8/15.
*
* All childes should have unique height.
* Set layout_height to wrap_content.
* RecyclerView
*/
public class HeightResizableLinearLayoutManager<T> extends LinearLayoutManager {
private int heightMeasure;
private List<T> dataSet;
public HeightResizableLinearLayoutManager(Context context, List<T> dataSet) {
super(context);
this.dataSet = dataSet;
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
measureScrapChild(recycler);
setMeasuredDimension(widthSpec, heightMeasure);
}
private void measureScrapChild(RecyclerView.Recycler recycler) {
if (dataSet.size() == 0) {
heightMeasure = 0;
return;
}
View view = recycler.getViewForPosition(0);
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(0,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(0,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
heightMeasure = view.getMeasuredHeight() * dataSet.size();
recycler.recycleView(view);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment