Android переработчик вид с 2 столбцами не выровнены - PullRequest
0 голосов
/ 06 июня 2018

Я пытаюсь создать представление рециркулятора с таким дизайном

enter image description here

Это одно представление Recycler, сложная часть здесь

enter image description here

Мы находимся во втором ряду представления переработчика, и мне нужно, чтобы второй ряд находился в середине правого элемента первого ряда.

Вот текущий код

<?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="horizontal">

        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1 "
            android:layout_height="140dp"
            android:layout_marginEnd="@dimen/margin_8"/>    

        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1 "
            android:layout_height="140dp"
            android:layout_marginStart="@dimen/margin_8"/>

</LinearLayout>

Возможно ли это сделать на Android?Я пытаюсь использовать отрицательное поле для левого элемента строки, но это не работает.

Я использую представление recylcler и базовый RecylcerView.Adapter для раздувания содержимого.

1 Ответ

0 голосов
/ 06 июня 2018

Вы можете использовать StaggeredGridLayoutManager . GridLayoutManager занимает одинаковое пространство для каждой ячейки.В отличие от GridLayoutManager, StaggeredGridLayoutManager занимают место для каждой ячейки столько, сколько им требуется.

Вы можете использовать 2 типа просмотра для RecyclerView.

  1. NormalView
  2. Вид с дополнительным верхним полем

Использовать 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 использования.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...