Я пытаюсь создать ListView
с 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
.Это ничего не меняет.