Skip to content

FarshidRoohi/recyclerview-infinite-loop

Repository files navigation

Infinite Scroll loop with RecyclerView

RecyclerView Adapter:

class RecyclerViewAdapter(private val items: List<String>) : RecyclerView.Adapter<RecyclerViewAdapter.RVViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RVViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val view = RvItemBinding.inflate(layoutInflater, parent, false)
        return RVViewHolder(view)
    }

    override fun onBindViewHolder(holder: RVViewHolder, position: Int) {
        val realPosition = position % items.size
        val item = items[realPosition]
        holder.bind(item)
    }

    override fun getItemCount(): Int {
        return items.size.toDouble().pow(10).toInt()
    }

    class RVViewHolder(private val binding: RvItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: String) {
            val page = itemView.resources.getString(R.string.page)
            binding.txtTitle.text = String.format(page, item)
        }
    }
}

Usage:

  private fun sampleWithRecyclerView() {
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        val items = getFakeItems()
        val adapter = RecyclerViewAdapter(items)
        recyclerView.adapter = adapter

        val snapHelper = PagerSnapHelper()
        snapHelper.attachToRecyclerView(recyclerView)

        val position = items.size.toDouble().pow(5).toInt()
        recyclerView.scrollToPosition(position)
    }
Screen_recording_20240225_154302.mp4

Releases

No releases published

Packages

No packages published

Languages