繁体   English   中英

有没有一种好方法可以在循环中用另一个线性膨胀cardview布局?

[英]Is there a good way to inflate cardview layout with another linear in a loop?

我试图用CardView创建一个ListView CardView总是包含3行,其中包含一些信息,但之后它得到2n行,如下所示:
-职位,名字;
-图像,数据,图像,数据。
我正在为此任务对象使用,其中包含:
-带有数据的对象,该数据将始终填充第3行;
-我用于2n行的对象列表。

我已经尝试过了:
-将RecyclerAdapter交换为ArrayAdapter (有助于我也更改可见性,但不会膨胀);
-创建一种方法来处理与扩大该布局有关的所有逻辑
-在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);
        }
    }

@EDIT: 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内部CardView添加对对象正确的数据,并连接到该对象列表。

@ EDIT2:我尝试仅手动创建View该部分,而不是使用LayoutInflater 那不会改变任何东西。

在与该主题有所分歧后,我找到了解决方法。 适配器重用getView / onBindViewHolder上的旧View 如果在添加新元素之前未清除包含其他元素(如TextViewImageView等)的较早列表的Linear Layout ,则旧的将保留。 解决方法是删除旧的。 在线Linear Layout我需要在添加新元素之前调用removeAllViews()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM