Есть шаблон элемента:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:background="@color/background_pin_themplate"
android:padding="15dp">
<ImageView
android:id="@+id/book_card_image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/design_default_color_secondary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/book_card_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/example_title"
android:textColor="@color/colorHeader"
android:textSize="30sp"
app:fontFamily="@font/roboto_medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/book_card_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
Есть код:
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
root = inflater.inflate(R.layout.fragment_home, container, false);
LinearLayout layout = root.findViewById(R.id.recommended);
for (int i = 0; i < 10; i++){
View view = inflater.inflate(R.layout.card_book_home, container, false);
TextView title = view.findViewById(R.id.book_card_text);
ImageView image = view.findViewById(R.id.book_card_image);
title.setText("Книга " + i);
image.setBackgroundResource(R.color.colorHeader);
Log.i("<<<<<<<<<<< I", String.valueOf(title.getText()));
layout.addView(view);
}
return root;
}
Код должен просто добавлять элементы в layout. И это происходит! Но, почему-то текст и изображение просто пропадают! Что делать?