再次获取数据时 Recyclerview 项目增加高度 Android Kotlin

Recyclerview item increase height when fetch data again Android Kotlin

我有recyerlView。我从服务器加载数据。当它最初加载时,它的高度是正常的,但是当我通过项目点击进入下一个 activity 时。然后回来慢慢增加 child 的高度。我尝试对此进行调试,发现 onResume api 调用导致了问题。但是我在布局上做错了什么我不明白。

FirstLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/reyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

FirstActivity.kt

class FirstActivity : BaseActivity() {

    lateinit var binding: FirstLayoutActivityLayoutBinding
    private val viewModel: FirstViewModel by inject()
    private var listAdapter: ListAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setupViewModel()
        binding = FirstLayoutActivityLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    private fun setupViewModel() {
        viewModel.livedata.observe(this, { list ->
            setupAdapter(list)
        })
    }

    private fun setupAdapter(list: List<String>) {
        initializeAdapter(list)
        listAdapter?.updateItemsList(list)
        binding.recyclerView.apply {
            addItemDecoration(HeaderItemDecoration(context))
             val itemDecorator = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
        itemDecorator.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider)!!)
        addItemDecoration(itemDecorator)
            adapter = listAdapter
        }
    }

    private fun initializeAdapter(list: List<String>) {
        listAdapter = ListAdapter(list.toMutableList())
    }

    override fun onResume() {
        super.onResume()
        viewModel.fetchItem() // noraml retrofit call
    }
}

HeaderItemDecoration 就是从这个.

使用的

ListAdapter.kt

class ListAdapter(private val list: MutableList<String>) : RecyclerView.Adapter<Adapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(
            ListLayoutBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bindItem(list[position])
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class MyViewHolder(private val binding: ListLayoutBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bindItem(s: String) {
            binding.cool.text = s
        }
    }
}

ListLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:background="@color/red"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/cool"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我正在将我的观点添加到我的 Youtube Link。请看看是什么问题。谢谢

我没有添加任何增加高度的逻辑

看起来像多个 ItemDecoration 的问题,每次获取新数据时都会设置这些。确切地说:多个 DividerItemDecorations,它向项目添加小填充(用于划分它们)。注意方法名称以 add... 开头,因此每次添加新的时,旧的都会保留在那里,并且此分隔符的每个实例都会添加一些填充。如果你会实施例如每次在后台下拉刷新或自动刷新,例如10 秒后,每次刷新 GUI 方法调用 (setupAdapter) 都会添加一些 space 而不会离开 Activity。目前您只在 onResume 中获取一次数据,因此每次 activity 移动到前台的操作都会添加一个分隔符(当数据被正确获取时)

将这部分代码移至 onCreate 以仅设置一次分隔符,这是通过代码进行一些额外样式设置的适当位置

binding.recyclerView.apply {
        addItemDecoration(HeaderItemDecoration(context))
        val itemDecorator = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
        itemDecorator.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider)!!)
        addItemDecoration(itemDecorator)
}

并在您的 setupAdapter 方法中仅将适配器设置为 RecyclerView,不要设置样式(多次)

private fun setupAdapter(list: List<String>) {
    initializeAdapter(list)
    listAdapter?.updateItemsList(list)
    binding.recyclerView.adapter = listAdapter
}