【发布时间】:2019-05-01 06:20:06
【问题描述】:
我正在尝试用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);
}
}
@编辑: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。这不会改变任何事情。
【问题讨论】:
-
请分享您的卡片 xml。此外,您在
getView中膨胀了两倍的R.layout.card。请编辑它。
标签: java android listview layout-inflater