Java 未以编程方式将CardView添加到LinearLayout

Java 未以编程方式将CardView添加到LinearLayout,java,android,xml,android-recyclerview,android-linearlayout,Java,Android,Xml,Android Recyclerview,Android Linearlayout,每次我为我的RecyclerView向适配器添加CardView时,它都会不断地将其添加到RecyclerView根目录,而不是我在其中创建的线性布局。我希望将我的CardView添加到LinearLayout ll_section_卡中,以便根据需要显示或隐藏卡。我已经设法用XML实现了这一点,但是如何通过编程实现呢?我还没有看到任何关于如何做到这一点的教程 当前结果 预期结果 片段类 需要布局编程版本 要将cardView添加到LinearLayout,您需要将cardView保留在单独的布

每次我为我的RecyclerView向适配器添加CardView时,它都会不断地将其添加到RecyclerView根目录,而不是我在其中创建的线性布局。我希望将我的CardView添加到LinearLayout ll_section_卡中,以便根据需要显示或隐藏卡。我已经设法用XML实现了这一点,但是如何通过编程实现呢?我还没有看到任何关于如何做到这一点的教程

当前结果

预期结果

片段类

需要布局编程版本


要将cardView添加到LinearLayout,您需要将cardView保留在单独的布局中:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv_content">

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

                <TextView
                     android:id="@+id/tv_cv_title"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/app_name" />

                 <TextView
                      android:id="@+id/tv_cv_subtitle"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/app_name" />
        </LinearLayout>

</android.support.v7.widget.CardView>
以下是项目列表:

List<Item> items = new ArrayList();
Item item = new Item(title, subTitle);
items.add(item);

可以从碎片中膨胀布局吗?我正在努力保持我的代码尽可能整洁。你可以自己尝试,但据我所知,这是不可能的。您需要在适配器中对其进行充气。您从何处获得物品?不需要在某处声明吗?如何设置这些文本视图的文本?我想您需要向LinearLayout添加几个CardView。为此,您可能有一个项目列表。列表Item是一个可以为这些文本视图设置文本的类。看看我编辑过的答案,我以前用过ArrayList。可以在片段中声明项目列表吗?我不想每次创建包含不同列表/项目名称的列表时都创建一个adpater。
public class SMSmessage {
    public String senderName, smsContent;

    public SMSmessage(String senderName, String smsContent) {
        this.senderName = senderName;
        this.smsContent = smsContent;
    }

    public String getSenderName() {
        return senderName;
    }

    public String getSmsContent() {
        return smsContent;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_sectionwithexpandability_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll_section_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">

        <TextView
            android:id="@+id/tv_sectionheader_expandcollapsearrow"
            android:clickable="true"
            android:focusable="true"
            android:layout_weight="10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp" />

        <TextView
            android:id="@+id/tv_sectionheader_title"
            android:layout_weight="90"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_section_cards"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/cv_content">

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

                <TextView
                    android:id="@+id/tv_cv_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name" />

                <TextView
                    android:id="@+id/tv_cv_subtitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
</LinearLayout>
public class SMSmessage {
    private String senderName, smsContent;

    public SMSmessage(String senderName, String smsContent) {
        this.senderName = senderName;
        this.smsContent = smsContent;
    }

    public String getSenderName() {
        return senderName;
    }

    public String getSmsContent() {
        return smsContent;
    }
}
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv_content">

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

                <TextView
                     android:id="@+id/tv_cv_title"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/app_name" />

                 <TextView
                      android:id="@+id/tv_cv_subtitle"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/app_name" />
        </LinearLayout>

</android.support.v7.widget.CardView>
ll_section_cards.removeAllViews();
for (int childPosition = 0; childPosition < items.size(); childPosition++) {
      LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View childView = infalInflater.inflate(R.layout.card_item, null);

      Item item = items.get(childPosition);
      TextView title = childView.findViewById(R.id.tv_cv_title);
      title.setText(item.getTitle());

      ll_section_cards.addView(childView);
}
class Item {

   public Item(String title, String subTitle) {
     this.title = title;
     this. subTitle = subTitle;
   }

   public String title;
   public String subTitle;
}
List<Item> items = new ArrayList();
Item item = new Item(title, subTitle);
items.add(item);