0

在我的 android 片段中,我有多个水平回收器视图,我想在为回收器视图加载数据的同时显示一些微光效果。所以我制作了一个 shimmer_layout.xml 文件,其中包含一个包含 3 个元素的线性布局,每个元素都有一个唯一的 id。为了使布局适应不同的设备屏幕,这些元素不能有固定的大小(宽度和高度),所以在显示 UI 之前我做了一些计算,至少为其中一个设置宽度和高度(其他有匹配的父属性和父有包装内容,以便一切都以编程方式与一组对齐)。这仅适用于一种布局。如何使用包含引用其他布局(相同的 shimmer_layout.xml)以编程方式设置宽度和高度?好像android只为第一个设置宽度和高度。

Fragment xml 在回收器视图之上包含两个(但可能更多)shimmer_layout.xml。加载数据后,这些将消失。


...

<LinearLayout
                android:id="@+id/layoutNow"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="24dp"
                android:layout_marginTop="32dp"
                android:layout_marginEnd="24dp"
                android:gravity="bottom|fill_horizontal"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tvNow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/now_in_theaters"
                    android:textFontWeight="700"
                    android:textSize="24sp" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <TextView
                    android:id="@+id/tvSeeAllNow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="@string/see_all" />
            </LinearLayout>


            <com.facebook.shimmer.ShimmerFrameLayout
                android:id="@+id/shimmer_frame_layout"
                android:layout_marginStart="24dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <HorizontalScrollView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

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

                </HorizontalScrollView>


            </com.facebook.shimmer.ShimmerFrameLayout>


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvNowMovies"
                android:layout_marginStart="24dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


            <LinearLayout
                android:id="@+id/layoutComing"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginStart="24dp"
                android:layout_marginEnd="24dp"
                android:gravity="bottom|fill_horizontal"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tvComing"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/coming_soon"
                    android:textFontWeight="700"
                    android:textSize="24sp" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <TextView
                    android:id="@+id/tvSeeAllComing"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="@string/see_all" />
            </LinearLayout>


        <com.facebook.shimmer.ShimmerFrameLayout
            android:id="@+id/shimmer_frame_layout_2"
            android:layout_marginStart="24dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

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

            </HorizontalScrollView>


        </com.facebook.shimmer.ShimmerFrameLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvComingMovies"
                android:background="@color/black"
                android:layout_marginStart="24dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

    </LinearLayout>

...


shimmer_layout.xml。我知道我可以只使用一个垂直线性布局并在片段中的包含标签中重复这 3 次,但这样,同样的故事,我只能为行中的第一个元素设置宽度和高度。回收器视图元素的宽度和高度也可以通过编程方式设置,但没有问题,因为每个项目都是在适配器中单独呈现的。

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/profileImage"
            android:layout_width="150dp"
            android:layout_height="250dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"
            android:lines="1"
            android:layout_marginTop="8dp"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

        <TextView
            android:id="@+id/rat"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"
            android:lines="1"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="36dp"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/profileImage2"
            android:layout_width="150dp"
            android:layout_height="250dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"
            android:lines="1"
            android:layout_marginTop="8dp"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"
            android:lines="1"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="36dp"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_marginEnd="24dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/profileImage3"
            android:layout_width="150dp"
            android:layout_height="250dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"
            android:lines="1"
            android:layout_marginTop="8dp"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:background="@drawable/rounded_background_poster"
            android:backgroundTint="@color/gray"
            android:lines="1"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="36dp"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

    </LinearLayout>

</LinearLayout>

4

0 回答 0