首页 > 解决方案 > 有没有一种好方法可以用循环中的另一个线性来膨胀 cardview 布局?

问题描述

我正在尝试创建一个ListViewwith CardViewCardView总是包含 3 行和一些信息,但之后它有 2n 行看起来像:
- 位置,名称;
- 图像,数据,图像,数据。
我正在使用这个任务对象,它包含:
- 带有数据的对象,它将始终填充拳头 3 行;
- 对象列表,我用于 2n 行。

我已经尝试过:
- 交换RecyclerAdapterArrayAdapter(有助于我改变的可见性,但不是膨胀);
- 创建一个方法来处理与膨胀该布局相关的所有逻辑
- 膨胀内部onBindViewHolder/getView

CardView我将以另一种方法粘贴带有膨胀的版本:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {    

        /*inflating layout and fill it with data from first object*/
        View listItem = convertView;
        if(listItem == null)
            listItem = LayoutInflater.from(context).inflate(R.layout.card,parent,false);
        //add data

        //if needed to make sure that inflating will occur once. list is 
        //LinearLayout inside CardView, current is entire object
        if(list.getChildCount() < 1)
                addList(list, current);


        //setting click listeners and returning view
    }

 private void addList(ViewGroup parent, ListItem current){
        for (Item var : ListItem.getItemList()) {
            View layout = LayoutInflater.from(context).inflate(R.layout.card_part, parent, false);

            //setting data

            ViewGroup.LayoutParams params = layout.getLayoutParams();
            params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
            layout.setLayoutParams(params);
            parent.addView(layout);
        }
    }

@编辑:CardView

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/colorPrimary"
    android:layout_margin="15dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="25dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/id"
            android:visibility="gone"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/text"
            android:id="@+id/name"
            android:textSize="20sp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/text"
            android:id="@+id/type"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textColor="@color/text"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_expand"
                tools:ignore="ContentDescription"
                android:id="@+id/show_list"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_hide"
                tools:ignore="ContentDescription"
                android:visibility="gone"
                android:id="@+id/hide_list"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/list"
            android:visibility="gone">

        </LinearLayout>


    </LinearLayout>
</android.support.v7.widget.CardView>

实际结果:
- 如果我评论

if(list.getChildCount() < 1)

数据填充有时会添加几次,不仅来自正确的对象。
- 现在使用该if布局正在使用错误的数据进行膨胀。
预期结果:
在内部CardView添加对对象正确的数据并连接到对象列表。

@EDIT2:我试图View手动创建那部分而不是使用LayoutInflater. 这不会改变任何事情。

标签: javaandroidlistviewlayout-inflater

解决方案


在从这个话题中休息一段时间后,我找到了解决方法。适配器重用旧ViewgetView/ onBindViewHolder。如果在添加新元素之前未清除其他元素(如 等)的早期列表,则旧元素将保留Linear Layout。解决方法是删除旧的。在 添加新元素之前我需要调用。TextViewImageViewLinear LayoutremoveAllViews()


推荐阅读