无尽的滚动RecyclerView给出了两次随机项目

问题描述 投票:0回答:1

我正在努力让 RecyclerView 显示来自园艺技巧大 ArrayList 的数据。 RecyclerView 行是全屏的。当用户滚动到列表末尾时,应拉出随机项目。

我已经半工作了。但问题是

existingTipsList.add(newRandomTip) (and the print statement below it)
每隔一次滑动就会被调用,这会导致数据重复两次。

这是我的进步:

适配器

class GardeningTipAdapter(val dataSource:ArrayList<GardeningTipsModel>) : RecyclerView.Adapter<GardeningTipAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view: View = LayoutInflater.from(parent.context).inflate(R.layout.gardening_tips_row, parent, false)
        return ViewHolder(view)
    }
    override fun getItemCount(): Int {
        return dataSource.size
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val text = dataSource[position].gardeningTipFull
        holder.label.text = text
    }
    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val label: TextView = itemView.findViewById(R.id.gardening_tips_row)
    }
}

型号

object GardeningTipsData {

    fun allGardeningTips() : ArrayList<TipsModel>{
        return arrayListOf(

            TipsModel("Gardening Tip 1"),
            TipsModel("Gardening Tip 2"),
            TipsModel("Gardening Tip 3"),
            TipsModel("Gardening Tip 4"),
            TipsModel("Gardening Tip 5"),
            TipsModel("Gardening Tip 6"),

            )
    }

    fun getRandomTip(currentTips: ArrayList<TipsModel>): ArrayList<TipsModel> {
        val allTipsList = allGardeningTips()
        val randomIndex = (0 until allTipsList.size).random()
        val randomTip = allTipsList[randomIndex]

        currentTips.add(randomTip)

        return currentTips
    }

}

滚动监听器

private val existingTipsList = ArrayList<TipsModel>()

    // Initial setup with a single random tip
    existingTipsList.add(GardeningTipsData.getRandomTip(existingTipsList).last())
    binding.tipRecyclerView.adapter = GardeningTipAdapter(existingTipsList)

    var loading = true
    var pastVisiblesItems: Int = 0
    var visibleItemCount: Int = 0
    var totalItemCount: Int = 0

    binding.tipRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            if (dy > 0) { // check for scroll down
                visibleItemCount = mLayoutManager.childCount
                totalItemCount = mLayoutManager.itemCount
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition()

                if (loading) {
                    if (visibleItemCount + pastVisiblesItems >= totalItemCount && !lastItemAdded) {
                        loading = false

                        // Get a new random item and append it to the list
                        val newRandomTip = GardeningTipsData.getRandomTip(existingTipsList).last()
                        existingTipsList.add(newRandomTip)
                        println("ADDED NEW!")

                        // Notify the adapter that the data has changed
                        binding.tipRecyclerView.adapter?.notifyItemInserted(existingTipsList.size - 1)

                        lastItemAdded = true
                        loading = true
                    } else {
                        lastItemAdded = false
                    }
                }
            }
        }
    })

通过此设置,当您滚动 RecyclerView 时,您会看到以下内容:

Gardening Tip 5
Gardening Tip 5
Gardening Tip 1
Gardening Tip 1
Gardening Tip 4
Gardening Tip 4
Gardening Tip 2
Gardening Tip 2
Gardening Tip 2
Gardening Tip 2
Gardening Tip 1
Gardening Tip 1

目标是随机显示数据,如下所示:

Gardening Tip 5
Gardening Tip 4
Gardening Tip 1
Gardening Tip 3
Gardening Tip 6
Gardening Tip 1
Gardening Tip 2
Gardening Tip 3
Gardening Tip 4
Gardening Tip 5
Gardening Tip 2
Gardening Tip 5

我相信问题是每次将新项目附加到 arrayList 时,它都会添加两次,这会导致重复。我如何调整它,以便数据随机显示,并且当用户点击最后一个项目时,

existingTipsList.add(newRandomTip)
被调用?

android kotlin android-recyclerview
1个回答
0
投票

您将添加到作为参数发送的

existingTipsList
列表中,同时使用
currentTips.add(randomTip)
函数中的
getRandomTip
existingTipsList.add(newRandomTip)
行。请注意您正在添加到同一列表引用?请尝试以下解决方案。

fun getRandomTip(currentTips: ArrayList<TipsModel>): ArrayList<TipsModel> {
    val allTipsList = allGardeningTips()
    val randomIndex = (0 until allTipsList.size).random()
    val randomTip = allTipsList[randomIndex]
    
    val tips = arrayListOf<TipsModel>()
    tips.addAll(currentTips)
    tips.add(randomTip)

    return tips
}
© www.soinside.com 2019 - 2024. All rights reserved.