java - 有没有一种好方法可以用循环中的另一个线性来膨胀卡片 View 布局?

标签 java android listview layout-inflater

我尝试使用 CardView 创建一个 ListViewCardView 始终包含 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);
        }
    }

@编辑: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 内部膨胀添加适合对象的数据并连接到它的对象列表。

@编辑2: 我尝试手动创建 View 的该部分,而不是使用 LayoutInflater。这不会改变任何事情。

最佳答案

在离开这个主题一段时间后,我找到了方法。适配器在 getView/onBindViewHolder 上重用旧的 View。如果在添加新元素之前未清除包含其他元素(例如 TextViewImageView 等)的早期列表的 Linear Layout,则旧元素将保留。 解决方案是删除旧的。在Linear Layout上,我需要在添加新元素之前调用removeAllViews()

关于java - 有没有一种好方法可以用循环中的另一个线性来膨胀卡片 View 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55932370/

相关文章:

java - 为 Weblogic 集群中的节点启用调试

java - LinearLayout 不测量内部的 TextView

java - Android 相机,内部列表选项?

java - 可观察链死亡和 Intellij 滞后/卡住

java - 将字母电话号码转换为数字

android - gradle 构建变体的文件夹命名约定

Android maps.googleapis.com,检索 JSON 数据时出现问题

android - SQLiteOpenHelper getInstance() 方法抛出空指针异常

java - ListView setCellFactory 会搞乱绑定(bind),除非匿名内部类?

java - 我正在尝试按字母顺序排列我的 ListView