StaggeredGridLayoutManager: Добавление поля делает элементы макета смещенными в сторону - PullRequest
0 голосов
/ 12 сентября 2018

Итак, я работаю над небольшим проектом и хочу добавить StaggeredGridLayoutManager в файл. Хотя это работает, если я не добавляю itemDecorator и не margin или padding в мои макеты. Как только я добавляю поля, это сдвигает список в одну сторону. Я прилагаю скриншот, чтобы прояснить проблему. enter image description here



Вот мой код:

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 ]

0 голосов
/ 28 июля 2019

В котлин используется:

viewManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
0 голосов
/ 20 декабря 2018

Я не знаю, нашли ли вы уже решение, но я оставлю комментарий на тот случай, если кто-нибудь еще увидит вопрос.

Мне удалось решить эту проблему, выполнив следующие действия:

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