I am working on getting a RecyclerView to display data from a big ArrayList of gardening tips. The RecyclerView row is full screen. When user scrolls to the end of the list, a random item should be pulled.
I have it half-working. But the problem is
Код: Выделить всё
existingTipsList.add(newRandomTip) (and the print statement below it)
Here is my progress:
Adapter
Код: Выделить всё
class GardeningTipAdapter(val dataSource:ArrayList) : RecyclerView.Adapter() {
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{
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): ArrayList {
val allTipsList = allGardeningTips()
val randomIndex = (0 until allTipsList.size).random()
val randomTip = allTipsList[randomIndex]
currentTips.add(randomTip)
return currentTips
}
}
Код: Выделить всё
private val existingTipsList = ArrayList()
// 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
}
}
}
}
})
Код: Выделить всё
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
Код: Выделить всё
existingTipsList.add(newRandomTip)
Источник: https://stackoverflow.com/questions/781 ... item-twice