为了账号安全,请及时绑定邮箱和手机立即绑定

StaggeredGridLayoutManager:添加Margin使布局项被推到一边

StaggeredGridLayoutManager:添加Margin使布局项被推到一边

烙印99 2021-09-15 14:39:10
所以,我正在做一个小项目,我想添加StaggeredGridLayoutManager到文件中。如果我不在我的布局中添加itemDecoratorand nomargin或padding,它会起作用。一旦我添加了边距,它就会将列表推到一侧。我附上屏幕截图以澄清问题。这是我的代码: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>我已经环顾了一段时间,但无法找到解决问题的方法。任何帮助表示赞赏。谢谢。
查看完整描述

2 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

我不知道你是否已经找到了解决方案,但我会留下评论,以防其他人看到这个问题。


我能够通过执行以下操作来解决这个问题:


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);


查看完整回答
反对 回复 2021-09-15
?
PIPIONE

TA贡献1829条经验 获得超9个赞

在 kotlin 中使用:

viewManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE


查看完整回答
反对 回复 2021-09-15
  • 2 回答
  • 0 关注
  • 510 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信