Android 动态添加cardview到水平滚动视图

Android dynamically add cardview into horizontal scroll view

我正在尝试将卡片视图动态添加到水平滚动视图中,以便我拥有与数据库中的行一样多的卡片视图。我在单独的 XML 文件中定义了 cardview。

但是我无法将卡片视图添加到主 activity 的布局中,因为卡片视图为空。添加来自不同文件的元素有问题吗?我应该在代码中定义卡片视图吗?或者我应该改用 RecyclerView 吗?

我搜索了很多,但没有任何帮助。

这是我的主要activity。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout scroll =  findViewById(R.id.layout_horizontal_theme);

        CardView cardView = findViewById(R.id.theme_cardview);

        scroll.addView(cardView);

    }

XML 卡片视图文件

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="20dp"
    android:contentDescription="@string/card"
    android:minHeight="150dp"
    app:cardBackgroundColor="#FF402D"
    app:cardCornerRadius="16dp"
    android:id="@+id/theme_cardview">

    <!--    app:cardElevation="10dp"-->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:id="@+id/layout_theme_card">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:contentDescription="@string/card_image"
            android:cropToPadding="true"
            android:maxWidth="100dp"
            android:src="@drawable/android_developer" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:contentDescription="@string/card_theme"
            android:text="@string/card_theme"
            android:textColor="@color/Not_so_white_white"
            android:textSize="16sp"
            android:textStyle="bold" />

    </LinearLayout>


</androidx.cardview.widget.CardView>

和 XML 主要 activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        tools:layout_gravity="center">

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:layout_constraintBottom_toTopOf="@+id/horizontalScrollView"
            app:layout_constraintEnd_toEndOf="@id/guideline2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="@id/guideline"
            app:layout_constraintTop_toTopOf="parent">

            <include layout="@layout/progress_bar" />
        </RelativeLayout>


        <HorizontalScrollView
            android:id="@+id/horizontalScrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/list_scroll"
            android:fillViewport="true"
            app:layout_constraintBottom_toTopOf="@+id/horizontalScrollView2"
            app:layout_constraintEnd_toEndOf="@id/guideline2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="@id/guideline"
            app:layout_constraintTop_toBottomOf="@+id/relativeLayout">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:id="@+id/layout_horizontal_diff">

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

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

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

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

            </LinearLayout>
        </HorizontalScrollView>


        <HorizontalScrollView
            android:id="@+id/horizontalScrollView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/list_scroll_2"
            android:fillViewport="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/horizontalScrollView"
            app:layout_constraintVertical_bias="0.503">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:id="@+id/layout_horizontal_theme">

               <!-- <include layout="@layout/theme_card" />

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

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

                <include layout="@layout/theme_card" />-->

            </LinearLayout>

        </HorizontalScrollView>

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.10" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.90" />


    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
         android:id="@+id/navDrawer"
         android:layout_height="match_parent"
         android:layout_width="wrap_content"
         app:headerLayout="@layout/navigation_drawer"
         app:menu="@menu/nav_drawer_menu"
         android:layout_gravity="start"
         android:fitsSystemWindows="true"/>


</androidx.drawerlayout.widget.DrawerLayout>

谢谢大家的帮助:)

您需要扩充您的卡片视图,然后您可以将其用于任何目的:

View view = View.inflate(this, R.layout.cardview_layout, null);

然后您可以将其投射到 CardView

CardView cardView = (CardView) View.inflate(this, R.layout.cardview_layout, null);

您的代码将如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     LinearLayout scroll =  findViewById(R.id.layout_horizontal_theme);
        
     CardView cardView = (CardView) View.inflate(this, R.layout.cardview_layout, null);

     scroll.addView(cardView);

}

按照 example 创建自定义视图。之后,创建 您的自定义视图并将它们置于水平视图中。

CardView cardView = findViewById(R.id.theme_cardview); 总是 return 空。