Вы можете использовать StaggeredGridLayoutManager . GridLayoutManager занимает одинаковое пространство для каждой ячейки.В отличие от GridLayoutManager
, StaggeredGridLayoutManager
занимают место для каждой ячейки столько, сколько им требуется.
Вы можете использовать 2 типа просмотра для RecyclerView
.
- NormalView
- Вид с дополнительным верхним полем
Использовать 2-й вид только для позиции 1. Поскольку только вид с позиции 1 содержит дополнительное верхнее поле, которое вы увидитеВо втором столбце первого элемента есть дополнительное пространство сверху.Я думаю, что вы можете решить этот путь.
Переопределить getItemViewType
метод
@Override
public int getItemViewType(int position) {
if (position == 1)
return 1;
return 0;
}
в onCreateViewHolder
метод
if (viewType == 0){
// inflate your regular layout
}else{
// inflate layout with extra top margin
}
Пример макета XML:
обычный макет:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="@drawable/album_8"
/>
<TextView
android:id="@+id/textViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
android:singleLine="true"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/textViewSubTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:singleLine="true"
android:text="TextView"
android:textSize="11sp" />
</LinearLayout>
</LinearLayout>
Макет с дополнительным верхним полем:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="60dp"
android:layout_marginBottom="8dp"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="@drawable/album_8"
/>
<TextView
android:id="@+id/textViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
android:singleLine="true"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/textViewSubTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:singleLine="true"
android:text="TextView"
android:textSize="11sp" />
</LinearLayout>
</LinearLayout>
Проверьте этот пример репо для StaggeredGridLayoutManager
и некоторые дополнительные RecyclerView
использования.