0

I have a recylerview with an arrayList that contain all the day of the week. When I tap to select the day, the recyclerview is shown. Then, the third day of the week is already selected(by default). I need to center it to the recyclerview and then, when I selected another day in my recycler, it need to re-center again with the selected item. That's my code:

        val recyclerViewGiorni = view.giorniRV
        val horizontalLayoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        recyclerViewGiorni.layoutManager = horizontalLayoutManager
        adapter = ButtonPromemoriaAdapter(giorniPromemoria)
        recyclerViewGiorni.adapter = adapter
        val snapHelper = LinearSnapHelper()
        snapHelper.attachToRecyclerView(recyclerViewGiorni)
        recyclerViewGiorni.addOnScrollListener(object : RecyclerView.OnScrollListener() {

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    val centerView = snapHelper.findSnapView(horizontalLayoutManager)
                    val pos = horizontalLayoutManager.getPosition(centerView!!)
                    Log.e("Snapped Item Position:", "" + pos)
                }
            }
        })
Nicola
  • 301
  • 3
  • 20

1 Answers1

0

I think you're looking for a SpanHelper

SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(yourRecyclerView);

Please look at

for more info.

Demigod
  • 5,073
  • 3
  • 31
  • 49