java - 有没有一种好方法可以用循环中的另一个线性来膨胀 cardview 布局?
问题描述
我正在尝试创建一个ListView
with CardView
。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);
}
}
@编辑: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
. 这不会改变任何事情。
解决方案
在从这个话题中休息一段时间后,我找到了解决方法。适配器重用旧View
的getView
/ onBindViewHolder
。如果在添加新元素之前未清除其他元素(如 等)的早期列表,则旧元素将保留Linear Layout
。解决方法是删除旧的。在 添加新元素之前我需要调用。TextView
ImageView
Linear Layout
removeAllViews()
推荐阅读
- html - 页框已损坏
- c++ - 用二维字符数组的内容填充字符串的 C++ 向量:大小太大时失败
- c# - 如何重构当前使用字典的 C# 代码以减少冗余并提高类型安全性?
- python - 如何从python中的文本文件中获取函数参数?
- angular - Angular 服务将字符串从 App 组件共享到其他组件
- date - 使用 ARRAYFORMULA 插入新行时谷歌表格日期重新格式化
- python - 有什么方法可以在 Django 中为我的 sqlite3 数据库设置密码?
- asp.net-core - 如何从 asp.net core 3 Identity 中删除 UserName 字段
- html - 如何从网站获取 HTML 代码并将其转换为 VB 字符串?
- javascript - 可以在没有 ajaxSetup 的情况下预定义 ajax 参数吗?