Почему карточки в RecyclerView с большим отступом после первой прокрутки
Есть активити:
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Есть фрагмент:
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints"
tools:listitem="@layout/first" />
</androidx.constraintlayout.widget.ConstraintLayout>
И есть карточки (в примере покажу одну, остальные такие же, просто где-то нет, например, картинки):
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.card.MaterialCardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="12px"
app:layout_constraintTop_toTopOf="parent"
app:strokeColor="@color/black"
app:strokeWidth="1px"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="194dp"
android:contentDescription="desc"
android:scaleType="centerCrop" />
<LinearLayout
android:id="@+id/bag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Awesome title"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Awesome subtitle"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
При первом запуске карточки идут друг за другом, пока не прокрутишь вниз. После прокрутки вниз, до последней карточки, а потом вверх, между карточками появляется гигантский отступ. В чём может быть проблема? Фрагмент в активити добавляю так:
val ft = supportFragmentManager.beginTransaction()
val fragmentTest = MainFragment(service.getCards(receivedModels))
ft.replace(R.id.fragmentContainerView, fragmentTest)
ft.commit()
На скриншотах показано странное поведение
class MainFragment(val cards : MutableList<AbstractCard>) : Fragment() {
private lateinit var binding: FragmentMainBinding
private lateinit var adapter: CardAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentMainBinding.inflate(inflater, container, false)
adapter = CardAdapter()
binding.items.adapter = adapter
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.items.apply {
layoutManager = LinearLayoutManager(context)
adapter = adapter
}
adapter.submitList(cards)
}
}
Код фрагмента
Источник: Stack Overflow на русском