简体   繁体   English

如何在水平回收站视图中居中选定的项目?

[英]How to center selected item inside an horizontal recyclerview?

I have a recylerview with an arrayList that contain all the day of the week. 我有一个带有arrayList的recylerview,其中包含一周中的所有时间。 When I tap to select the day, the recyclerview is shown. 当我点击选择日期时,将显示recyclerview。 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. 我需要将其居中到recyclerview中,然后,当我在回收商中选择另一天时,它需要再次将所选项目重新居中。 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)
                }
            }
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM