【问题标题】:Duplicated recyclerview重复的回收站视图
【发布时间】:2020-10-02 14:51:22
【问题描述】:

我正在尝试创建一个包含 1 到 250 之间整数的回收器列表视图。

我遇到的问题是,当我滚动列表时,它只显示 1-9,然后随机只显示单个数字。是回收缓存的item值吗?

这是我的适配器的外观:

package com.work.me

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.counter_layout.view.*

class CounterAdapter : RecyclerView.Adapter<CounterAdapter.ViewHolder>() {

    private val counterList = mutableListOf<Int>()

    init {
        for (x in 0..COUNTER_MAX) {
            Log.d("JJJ", "x is $x")
            counterList.add(x + COUNTER_OFFSET)
        }
    }

    inner class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {

        fun bindView(counterValue: String) {
            Log.d("JJJ", "counterValue is $counterValue")
            view.counterText.text = counterValue
            Log.d("JJJ", "  view.counterText is ${view.counterText.text}")
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.counter_layout, parent, false)

        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        Log.d("JJJ", "size is " + counterList.size)
        return counterList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindView(counterList[position + COUNTER_OFFSET].toString())
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    companion object {
        const val COUNTER_MAX = 250
        const val COUNTER_OFFSET = 1
    }
}

如您所见,我已将日志放置在 bindView 函数上,传递的值是正确的,但从未显示在实际列表 ui 小部件上。

这是我使用适配器启动列表小部件的方式:

      counterList.apply {
            adapter = counterAdapter
//            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(this@MainActivity)
        }

这里是每个项目的柜台布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/counterText"
        style="@style/TextAppearance.AppCompat.Headline" />


</androidx.constraintlayout.widget.ConstraintLayout>

活动布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    ......

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/counterList"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/counterButton" />

</androidx.constraintlayout.widget.ConstraintLayout>

提前致谢

【问题讨论】:

  • 删除override fun getItemId(),因为您没有更改稳定ID
  • 删除无效

标签: android android-layout android-recyclerview


【解决方案1】:

我认为您在持有人中有旧的视图参考。
尝试摆脱它并改用itemView

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    fun bindView(counterValue: String) {
        Log.d("JJJ", "counterValue is $counterValue")
        itemView.counterText.text = counterValue
        Log.d("JJJ", "  view.counterText is ${itemView.counterText.text}")
    }
}

【讨论】:

  • @Jonathan 尝试删除 kotlin 合成并改用视图绑定。