0

Hello I have a horizontal recyclerView that should display a picture and below it a name. I can't get this to work as the image views appear of different sizes, even though they have fixed dimensions, and the text below is either not visible or weirdly placed. Below are the screenshots from the emulator and my phone.

image form emulator

image from phone while loading (you can see the text behind??)

image from phone after loading

Code:

The cardView where the recyclerview is located

<android.support.v7.widget.CardView
                android:id="@+id/more_cardView_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:visibility="visible"
                android:visibility="invisible"
                android:layout_marginRight="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginBottom="8dp">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_marginLeft="8dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"
                        android:textColor="@android:color/black"
                        android:text="Cast"
                        android:layout_marginBottom="4dp"/>

                    <android.support.v7.widget.RecyclerView
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginBottom="8dp"
                        android:id="@+id/cast_recyclerView"
                        android:layout_width="wrap_content"
                        android:layout_height="135dp">


                    </android.support.v7.widget.RecyclerView>
                </LinearLayout>

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

The list_item xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="100dp"
              android:background="#E0E0E0"
              android:layout_height="135dp">

    <ImageView
        android:id="@+id/profile_pic"
        tools:background="@android:color/black"
        tools:src="@drawable/test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"/>

    <TextView
        android:layout_marginTop="5dp"
        android:id="@+id/actor_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:maxLines="1"
        android:ellipsize="end"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        tools:text="The name of an actor"
        android:textSize="12sp"
        />

    <TextView
        android:id="@+id/actor_role"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:maxLines="1"
        android:ellipsize="end"
        tools:text="The character playd by that actor"
        android:textSize="12sp"

        />


</LinearLayout>

The preview of this file (this is the correct one taht should be displayed)

fgoogle
  • 79
  • 9

1 Answers1

2

For a horizontal recycler view,you can do something like the following :

LinearLayoutManager horizontalLayoutManagaer
            = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
cast_recyclerView.setLayoutManager(horizontalLayoutManagaer);
cast_recyclerView.setAdapter(horizontalAdapter);

And change your list_item.xml to the following :

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:background="#E0E0E0"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/profile_pic"
        tools:background="@android:color/black"
        tools:src="@drawable/test"
        android:layout_width="100dp"
        android:layout_height="135dp"
        android:scaleType="centerCrop"/>

    <TextView
        android:layout_marginTop="5dp"
        android:layout_gravity="center"
        android:id="@+id/actor_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:maxLines="1"
        android:ellipsize="end"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        tools:text="The name of an actor"
        android:textSize="12sp"/>

    <TextView
        android:id="@+id/actor_role"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:maxLines="1"
        android:ellipsize="end"
        tools:text="The character playd by that actor"
        android:textSize="12sp"/>
</LinearLayout>

Also change the recyclerview to the following in your cardView :

<android.support.v7.widget.RecyclerView
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:id="@+id/cast_recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView> 
Ayan
  • 5,592
  • 3
  • 30
  • 37