列表项视图未正确显示在线性 RecyclerView 中

List item views not correctly appearing in Linear RecyclerView

您好,我有一个水平的 recyclerView,它应该显示一张图片并在其下方显示一个名称。我无法让它工作,因为图像视图显示不同大小,即使它们具有固定尺寸,并且下面的文本不可见或奇怪地放置。以下是模拟器和我的 phone.

的屏幕截图

image form emulator

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

image from phone after loading

代码:

recyclerview所在的cardView

<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>

list_itemxml

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

对于水平回收器视图,您可以执行如下操作:

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

并将您的 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="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>

同时将您的 cardView 中的 recyclerview 更改为以下内容:

<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>