Динамический GridLayoutManager. recyclerView
Для работы приложения я использую recyclerView. Возможно ли реализовать динамический GridLayoutManager, при котором при изменении размеров экрана телефона менялось и значение spanCount?
private fun configureRecyclerView() {
val layoutManager = GridLayoutManager(activity, GRID_SPAN_COUNT, GridLayoutManager.HORIZONTAL, false)
val options = getFirestoreRecyclerOptions()
adapter = NoticeAdapter(options, noticeViewModel)
binding.recyclerViewNotice.layoutManager = layoutManager
binding.recyclerViewNotice.adapter = adapter
}
const val GRID_SPAN_COUNT = 3
Скрин приложения (https://i.stack.imgur.com/7CouU.png)
Ответы (1 шт):
Автор решения: Mr.Krotos
→ Ссылка
Попробуйте использовать вот это вот:
public class AutoFitGridLayoutManager extends GridLayoutManager {
private int columnWidth;
private boolean columnWidthChanged = true;
public AutoFitGridLayoutManager(Context context, int columnWidth) {
super(context, 1);
setColumnWidth(columnWidth);
}
public void setColumnWidth(int newColumnWidth) {
if (newColumnWidth > 0 && newColumnWidth != columnWidth) {
columnWidth = newColumnWidth;
columnWidthChanged = true;
}
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (columnWidthChanged && columnWidth > 0) {
int totalSpace;
if (getOrientation() == VERTICAL) {
totalSpace = getWidth() - getPaddingRight() - getPaddingLeft();
} else {
totalSpace = getHeight() - getPaddingTop() - getPaddingBottom();
}
int spanCount = Math.max(1, totalSpace / columnWidth);
setSpanCount(spanCount);
columnWidthChanged = true;
}
super.onLayoutChildren(recycler, state);
}
}
Вот так вот:
AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(context, 800);
recview.setLayoutManager(layoutManager.HORIZONTAL);