mihon/app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.java

68 lines
1.9 KiB
Java
Raw Normal View History

2016-01-15 15:18:19 +01:00
package eu.kanade.tachiyomi.widget;
2015-12-27 14:58:36 +01:00
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
public class AutofitRecyclerView extends RecyclerView {
private GridLayoutManager manager;
private int columnWidth = -1;
private int spanCount = 0;
public AutofitRecyclerView(Context context) {
super(context);
init(context, null);
}
public AutofitRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public AutofitRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
int[] attrsArray = {
android.R.attr.columnWidth
};
TypedArray array = context.obtainStyledAttributes(attrs, attrsArray);
columnWidth = array.getDimensionPixelSize(0, -1);
array.recycle();
}
manager = new GridLayoutManager(getContext(), 1);
setLayoutManager(manager);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
if (spanCount == 0 && columnWidth > 0) {
int spanCount = Math.max(1, getMeasuredWidth() / columnWidth);
manager.setSpanCount(spanCount);
}
}
public void setSpanCount(int spanCount) {
this.spanCount = spanCount;
if (spanCount > 0) {
manager.setSpanCount(spanCount);
}
}
public int getSpanCount() {
return manager.getSpanCount();
}
public int getItemWidth() {
return getMeasuredWidth() / getSpanCount();
}
2015-12-27 14:58:36 +01:00
}