StaggeredGridLayoutManager:添加边距使布局项被推到侧面 [英] StaggeredGridLayoutManager: Adding Margin makes the layout items pushed to the side
问题描述
所以,我正在做一个小项目,我想在文件中添加StaggeredGridLayoutManager
.如果我不在布局中添加itemDecorator
并且不添加margin
或padding
,则可以使用.一旦添加边距,它就会将列表推到一侧.我正在附上屏幕截图以澄清问题.
So, I'm working on a small project and I wanted to add StaggeredGridLayoutManager
to the file. While it works if I don't add an itemDecorator
and no margin
or padding
in my layouts. As soon as I add margins it pushes the list to one side. I'm attaching a screenshot to clarify the issue.
这是我的代码:
recyclerView.setHasFixedSize(true);
StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.addItemDecoration(new EqualSpaceItemDecoration(8));
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
以及该项目的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/wallpaper_item_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/cardview_light_background"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
app:cardPreventCornerOverlap="false">
<ImageView
android:id="@+id/wallpaper_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v7.widget.CardView>
</LinearLayout>
我已经环顾了一段时间,但找不到解决该问题的方法.任何帮助表示赞赏.谢谢.
I've looked around for some time and am not able to find something that fixes the issues. Any help is appreciated. Thanks.
推荐答案
我不知道您是否已经找到解决方案,但我会留下评论,以防其他人看到问题.
I don't know if you've already found the solution, but I'll leave the comment in case anyone else sees the question.
我能够通过以下操作解决此问题:
I was able to solve this problem by doing the following:
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
示例:
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);
这篇关于StaggeredGridLayoutManager:添加边距使布局项被推到侧面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!