Как центрировать предметы в Recyclerview? - PullRequest
0 голосов
/ 27 мая 2019

Я использую RecyclerView с элементами Cardview внутри.Я установил GridLayoutManager, чтобы он отображал 2 элемента в каждой строке, и я хочу, чтобы он центрировался на всех телефонах, например:

ожидаемый результат

expected result

Но у меня есть следующие данные:

Фактический результат

actual result

Я пробовал разные плохие решения (paddingLeft ...), ноон работает на телефонах с большими экранами и не работает на небольших телефонах.

content_main.xml выглядит следующим образом:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools">

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:queryHint="Rechercher un film"
        android:iconifiedByDefault="false"
        android:layout_marginTop="65dp"
        android:layout_marginBottom="10dp"
        android:layout_height="30dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/movies_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/searchView"
        android:scrollbars="vertical"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="true" />


</RelativeLayout>

movie_card.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">

<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="170dip"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/item_movie_poster"
            android:layout_width="170dip"
            android:layout_height="256dip"
            android:layout_centerHorizontal="true"
            android:contentDescription="@string/movie_image_description"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/item_movie_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/item_movie_poster"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dip"
            android:layout_marginTop="5dip"
            android:textSize="15sp"
            android:textAlignment="center"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textStyle="bold" />

    </RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

И app_bar_main.xml, который включает в себя content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

Ответы [ 4 ]

1 голос
/ 27 мая 2019

В вашем movie_card.xml установите ширину match_parent и добавьте гравитацию

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" //<-- change
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" //<-- add
android:orientation="vertical">
1 голос
/ 27 мая 2019

установить правое или левое поле в представлении recyler: -

<android.support.v7.widget.RecyclerView
        android:id="@+id/movies_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/searchView"
        android:scrollbars="vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="true" />
0 голосов
/ 27 мая 2019

Поскольку вы используете GridLayoutManager, установите card_view.xml ширину родительского вида как match_parent, т.е.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" //change
android:layout_height="wrap_content"
android:gravity="center"//add this
android:orientation="vertical">

Или вы можете сделать это RelativeLayout и установить для дочернего элемента CardView centerInParent значение true

Комментарий при любых запросах

Надеюсь, это поможет. Удачного кодирования!

0 голосов
/ 27 мая 2019

Проверьте ниже код.Вам нужно удалить android:layout_alignParentStart="true" и добавить android:layout_centerInParent="true" в ваш RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools">

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:queryHint="Rechercher un film"
        android:iconifiedByDefault="false"
        android:layout_marginTop="65dp"
        android:layout_marginBottom="10dp"
        android:layout_height="30dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/movies_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_centerInParent="true"
        android:layout_below="@+id/searchView"
        android:scrollbars="vertical"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="true" />

    </RelativeLayout>

Я надеюсь, что это сработает для вас.

...