Android RecyclerView自动调整布局管理器
我正在使用Android RecyclerView自动调整布局管理器,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,我正在使用RecyclerView和StaggedGridLayoutManager 我想让这个StaggedGridLayoutManager或任何LayoutManager占据空区域(如果有)。例如,如果我将spanCount设置为3 它必须占据所有屏幕宽度,即使我有2项或1项。在staggedGridLayoutManager中,我可以通过:setFullSpan(true)来满跨单行但不能仅为一行跨越两个项目 我的RecyclerView代码: StaggeredGridLayoutMa
RecyclerView
和StaggedGridLayoutManager
我想让这个StaggedGridLayoutManager
或任何LayoutManager
占据空区域(如果有)。例如,如果我将spanCount设置为3
它必须占据所有屏幕宽度,即使我有2项或1项。在staggedGridLayoutManager
中,我可以通过:setFullSpan(true)来满跨单行代码>但不能仅为一行跨越两个项目
我的RecyclerView代码
:
StaggeredGridLayoutManager sglm= new StaggeredGridLayoutManager(spanCount,StaggeredGridLayoutManager.VERTICAL);
sglm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
我试过了,但总是有一个空的区域
我收集了一些Facebook应用程序的截图:
这是Google Keep应用程序,你可以看到每一行的行高都是固定的,但项目的宽度是灵活的,我从来没有在Google Keep中看到过任何空白区域:
当我使用它时,总是有一个空白区域,正如你可以看到图像的黑色部分是空的。我希望RecyclerView
能够像Google Keep应用程序那样扩展我的行,从而占据该区域:
我访问了这个页面:但它对我没有帮助
这是我访问的另一个页面:
有没有人使用类似的RecyclerView
或视图?
有人能给我提供一些建议或想法,或者指导我从哪里开始吗?/**
/**
* A LayoutManager that lays out children in a staggered grid formation.
* It supports horizontal & vertical layout as well as an ability to layout children in reverse.
* <p>
* Staggered grids are likely to have gaps at the edges of the layout. To avoid these gaps,
* StaggeredGridLayoutManager can offset spans independently or move items between spans. You can
* control this behavior via {@link #setGapStrategy(int)}.
*/
/**
* Sets the gap handling strategy for StaggeredGridLayoutManager. If the gapStrategy parameter
* is different than the current strategy, calling this method will trigger a layout request.
*
* @param gapStrategy The new gap handling strategy. Should be
* {@link #GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS} or {@link
* #GAP_HANDLING_NONE}.
* @see #getGapStrategy()
*/
public void setGapStrategy(int gapStrategy) {
assertNotInLayoutOrScroll(null);
if (gapStrategy == mGapStrategy) {
return;
}
if (gapStrategy != GAP_HANDLING_NONE &&
gapStrategy != GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS) {
throw new IllegalArgumentException("invalid gap strategy. Must be GAP_HANDLING_NONE "
+ "or GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS");
}
mGapStrategy = gapStrategy;
setAutoMeasureEnabled(mGapStrategy != GAP_HANDLING_NONE);
requestLayout();
}
*一种布局管理器,它以交错的网格形式布局子对象。
*它支持水平和垂直布局以及反向布局子级的能力。
*
*交错网格可能在布局的边缘有间隙。为了避免这些差距,
*StaggedGridLayoutManager可以独立偏移跨距或在跨距之间移动项目。你可以
*通过{@link#setGapStrategy(int)}控制此行为。
*/
/**
*设置StaggedGridLayoutManager的间隙处理策略。如果GapstStrategy参数
*与当前策略不同,调用此方法将触发布局请求。
*
*@param gapStrategy新的差距处理策略。应该是
*{@link#间隙(处理)移动(项目)}或{@link
*#间隙(处理)(无)。
*@see#getGapStrategy()
*/
公共无效设置间隙策略(int间隙策略){
assertNotInLayoutOrScroll(空);
if(间隙策略==mGapStrategy){
返回;
}
如果(间隙策略!=间隙_处理_无&&
间隙策略!=间隙_处理_移动_项目_跨度之间){
抛出新的IllegalArgumentException(“无效的间隙策略。必须是间隙\u处理\u无”
+“或在跨度之间移动项目的间隙”;
}
mGapStrategy=间隙策略;
设置自动测量启用(mGapStrategy!=间隙处理无);
requestLayout();
}
在Git上检查此项
它可以帮助您设置所需的图像
在Play Store上查看此项
我最近在上看到了
若你们想达到和Google Keep应用程序一样的效果,你们可以使用它。若黑色部分是图像的一部分,我认为它无法被解析。不过,管理屏幕宽度和不留额外空间是可以解决的。@HammadTariq如果你看看谷歌Keep,它的每一行的行高都是固定的,而且它的图像是灵活的。所以你永远看不到它的黑色部分。我可以调整宽度,但我总是在RecyclerView
的底部看到额外的空间。那么,是否有任何方法或方法可以防止底部出现空格?是否要为所有行指定相等的高度?或者每行项目的高度应该相等?如果您希望所有行及其项目的高度相等,那么这是可能的。如果您查看Google,请保持第二行的行高度稳定。所有项目具有相同的高度,对于第三行,使用相同的规则。而且,如果我有两个行项目,甚至我的跨度计数3,这两个项目必须占据所有屏幕宽度。此外,项目的宽度是灵活的,而不是固定的。我将尝试此项目,并让您知道结果。谢谢你使用我老同事的项目。他是天才。已在一个项目中实施。太棒了。我试过你的方法,首先,即使我设置了android:layout\u width=“match\u parent”
它也不会占据所有屏幕宽度,而且它还将显示图像的数量限制为=4。以下是截图:。我想列出像无限的图像,它必须占据所有屏幕宽度。但与其他库相比,这个库很好。你可以在那里发表评论(Git),他会回复你的。