我试图创建一个布局管理器,将其水平放置,直到遇到回收站视图的宽度为止.如果到达边缘,则应在下一行中布置子代.
例如,假设在回收者视图中有4个项目-item1,item2,item3和item4.
item1和item2的视图彼此相邻放置.还剩下一些空间.但是item3的视图无法适应该宽度.因此item3转到下一行.但是现在剩下的差距应在项目1和项目2之间平均分配.
| <item1><item2><--gap-->||<-----item3----> |
这应该成为
| <--item1--> <--item2-->||<-----item3----> |
如果item4的视图适合在item3之后的空间内,则应将其放置在该位置.
| <--item1--> <--item2--> ||<-----item3----><-item4->|
这不能通过GrIDLayoutManager或StaggeredGrIDLayoutManager来实现,因为它们不考虑单个项目的宽度变化.
要编写自定义布局管理器,我有一种感觉,我应该重写布局管理器的onLayoutChildren方法.但是我有点卡住了.我不确定该怎么做.任何帮助,将不胜感激.
谢谢.
解决方法:
我遇到过类似的问题,但是使用GrIDLayout解决了这个问题.应当从一开始就创建所有视图的缺点:
<ScrollVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <androID.support.v7.Widget.GrIDLayout androID:ID="@+ID/bubble_grID" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:scrollbars="vertical" androID:scrollbarStyle="outsIDeOverlay" androID:layout_gravity="top|left" app:orIEntation="vertical" app:columnCount="@integer/grID_cells"> </androID.support.v7.Widget.GrIDLayout> </ScrollVIEw>
这是一个适配器:
public class GrIDAdapter { /** * Indicates maximum filled row in current column, column is the index of arr, row is the value * [224444555550000000] * It equals to: * [***********-------] * [***********-------] * [--*********-------] * [--*********-------] * [------*****-------] * [------------------] */ private final int[] mRowMatrix; public GrIDAdapter(GrIDLayout grIDLayout) { mGrIDLayout = grIDLayout; mPlacedList = new ArrayList<>(10); mUnplacedList = new linkedList<>(); mNumColumns = ResHelper.getInteger(R.integer.grID_cells); mMinCells = ResHelper.getInteger(R.integer.min_cells); mMaxCells = ResHelper.getInteger(R.integer.max_cells); mRowMatrix = new int[mNumColumns]; } public voID notifyDataSetChanged() { while (mUnplacedList.size() > 0) { final int toCol = findColWithMinRow(); final int gapSize = findGapSizeforCol(toCol); final CustomVIEw vIEw = findAppropriate(gapSize); if (vIEw == null) { final int filledRow = toCol > 0 ? (toCol + gapSize < mRowMatrix.length ? Math.min(mRowMatrix[toCol - 1], mRowMatrix[toCol + gapSize]) : mRowMatrix[toCol - 1]) : mRowMatrix[gapSize]; for (int j = toCol, jCount = toCol + gapSize; j < jCount; j++) { mRowMatrix[j] = filledRow; } } else { placeVIEw(vIEw, toCol, mRowMatrix[toCol]); } } } /** * Put vIEw in certain column and row in the grIDlayout */ private voID placeVIEw(CustomVIEw vIEw, int toCol, int toRow) { final int grIDSize = vIEw.getGrIDSize(); final GrIDLayout.LayoutParams params = new GrIDLayout.LayoutParams(); params.wIDth = params.height = grIDSize * mCellSize; params.columnspec = GrIDLayout.spec(toCol, grIDSize); params.rowSpec = GrIDLayout.spec(toRow, grIDSize); mPlacedList.add(vIEw); mUnplacedList.remove(vIEw); mGrIDLayout.addVIEw(vIEw, params); final int filledRow = toRow + grIDSize; for (int j = toCol, count = toCol + grIDSize; j < count; j++) { mRowMap[j] = filledRow; } } /** * Find empty gap which starts from toCol * [*********************] * [******------*****----] here col = 6, size = 3 * [******------*****----] * [******------*****----] * [*****************----] * @param toCol * @return */ private int findGapSizeforCol(int toCol) { final int fromrow = mRowMatrix[toCol]; int i = toCol; for (; i < mNumColumns; i++) { if (fromrow != mRowMatrix[i]) { break; } } return i - toCol; } /** * Find column with minimum filled row * [*********************] * [*****************----] here col = 17 * [******------*****----] * [******------*****----] * @return */ private int findColWithMinRow() { int minRow = Integer.MAX_VALUE, minCol = 0; for (int i = 0, count = mRowMatrix.length; i < count; i++) { if (minRow > mRowMatrix[i]) { minRow = mRowMatrix[i]; minCol = i; } } return minCol; } /** * Find customVIEw with appropriate size for the empty gap */ private CustomVIEw findAppropriate(int size) { for (int j = mUnplacedList.size() - 1; j >= 0; j--) { if (mUnplacedList.get(j).getGrIDSize() <= size) { return mUnplacedList.get(j); } } return null; }}
总结 以上是内存溢出为你收集整理的android-为RecyclerView创建可变列大小的布局管理器全部内容,希望文章能够帮你解决android-为RecyclerView创建可变列大小的布局管理器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)