ImageView в горизонтальном RecyclerView не показывает - PullRequest
0 голосов
/ 31 октября 2019

Имея horizontal recyclerView, элемент (CardItem) содержит imageView. Когда элемент отображается в vertical recyclerView, он работает нормально, но при изменении на horizontal recyclerView изображение не отображается.

это упрощенный элемент с изображением

         <ImageView
            android:id="@id/theImageVW"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

изображение будет установлено во время выполнения с помощью Glide fetch с удаленного компьютера, и трассировка показывает, что код вызывается.

<CardItem xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="fff"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:layout_marginStart="@dimen/margin_left"
        android:layout_marginEnd="@dimen/margin_right"
        android:visibility="visible">


        <FrameLayout
            android:id="@id/asset_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp">

            <ImageView
                android:id="@id/theImageVW"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:layout_alignWithParentIfMissing="true"
                android:layout_centerHorizontal="true"
                android:contentDescription="@null"
                android:visibility="visible"
                android:scaleType="fitCenter" />

            <androidx.cardview.widget.CardView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardCornerRadius="@dimen/corner_radius"
                app:cardElevation="0dp">

                    <FrameLayout
                        android:id="@id/video_container"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:visibility="gone" />

            </androidx.cardview.widget.CardView>

        </FrameLayout>

        <TextView
            android:id="@id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@id/asset_container"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:ellipsize="end"
            android:maxLines="2"
            />

        <RelativeLayout>
            ...
        </RelativeLayout>

        <RelativeLayout>
            ...
        </RelativeLayout>

    </RelativeLayout>

    ...

</FrameLayout>

одна вещь заметила widthMeasureSpec. переданный в

    onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

всегда равен 0 в horizontal recyclerView, и> 0 в vertical one. не уверен, что это является причиной, потому что при удалении этого override fun onMeasure() он все равно не показывает изображение, а imageView всегда имеет высоту и ширину как 0.

class CardItem : FrameLayout {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        val lp = theImageVW.layoutParams as RelativeLayout.LayoutParams
        val imageWidth = (View.MeasureSpec.getSize(widthMeasureSpec) -
            paddingLeft - paddingRight -
            lp.leftMargin - lp.rightMargin -
            getRootViewMarginLeft() -
            getRootViewMarginRight() -
            v.paddingLeft - v.paddingRight)

        lp.width = imageWidth
        lp.height = (imageWidth * aspectRatio).toInt()

        theImageVW?.scaleType = ImageView.ScaleType.FIT_CENTER

        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

    }
}

горизонтальный RecyclerView, как показано ниже, измените

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

не имеет значения

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RecyclerView
            android:id="@+id/hv_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:fillViewport = "true"
            />

        <ProgressBar
            android:id="@+id/progress_bar"
            style="?android:progressBarStyle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:visibility="gone" />

    </FrameLayout>

</merge>
...