How to smooth scroll to header in my recyclerview when clicking on tab

Viewed 300

I'm doing a feature like this image below

enter image description here

What I want to do is to select the corresponging tab when I'm about to reach my header.

And also navigate to the header when clicking the Tab itself

This is what I have done

private fun setupTabLayout(){
        storeAdapter.productList.groupBy ({ it.productHeader }, { product ->
            product
        }).iterator().forEach {
            tabLayout.addTab(tabLayout.newTab().setText(it.key))
        }

        tabLayout.addOnTabSelectedListener(object:TabLayout.OnTabSelectedListener{

            override fun onTabReselected(tab: TabLayout.Tab?) {}

            override fun onTabUnselected(tab: TabLayout.Tab?) {}

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val position = tab!!.position
                //Here I dont know how to get the position of the header to do recyclerview.smoothScrollToPosition(headerPosition)
            }
        })
    }

    private fun setupRecyclerViewListener(){
        recyclerView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            val itemPosition: Int = LinearLayoutManager(requireContext()).findFirstCompletelyVisibleItemPosition()
   //Here I dont know how to get the header position to select the corresponding tab

        }
    }

In the code you can see storeApdater.productList , this is my list of products , each product has a productHeader attribute which is a string in which I store that header.

The first code is to complete the tablayout with the corresponding headers, that is working fine, where I'm findind the hard part is to get the headers position to scroll to or select my tabs

0 Answers
Related