я пытаюсь решить следующий:RecyclerView с GridLayoutManager пытается решить wrap_content
- использования RecyclerView
- с GridLayoutManager
- с неподвижной сотовой шириной
- recyclerview изменение размер только на необходимую высоту (wrap_content)
Я стараюсь использовать следующий код, проблема в том, что он не работает, и я hav не обнаружил, что какой-либо рабочий пример не упоминается для правильной перерисовки при изменении ориентации.
public class AutofitRecyclerView extends RecyclerView {
private MyGridLayoutManager manager;
private int columnWidth = -1;
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 MyGridLayoutManager(getContext(), 1);
setLayoutManager(manager);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
if (columnWidth > 0) {
int mw = getMeasuredWidth();
int spanCount = Math.max(1, mw/columnWidth);
manager.setSpanCount(spanCount);
}
int numOfFullRows = manager.getItemCount()/manager.getSpanCount();
if(manager.getItemCount() % manager.getSpanCount() > 0)
{
numOfFullRows++;
}
if(getChildCount() > 0) {
int h = 0;
try {
h = manager.getChildAt(0).getMeasuredHeight();
} catch (Exception e) {
e.printStackTrace();
}
if(h != 0) {
getLayoutParams().height = numOfFullRows * h;
}
}
}
}
ссылка не работает больше –
@Viktor Я обновил URL – Arthur