水平RecyclerView中的ImageView不显示

时间:2019-10-30 23:13:59

标签: android android-layout android-recyclerview android-imageview horizontal-recyclerview

项目(CardItem)具有horizontal recyclerView,其中包含imageView。当项目显示在vertical recyclerView中时,它可以正常工作,但是当更改为horizontal recyclerView时,则不会显示图像。

这是带有图像的简化项

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

该图像将在运行时使用从远程获取的Glide获取,并且跟踪显示代码正在被调用。

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

始终为0,在vertical one

始终为0。 不知道是不是原因,因为删除此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>

0 个答案:

没有答案