M Tayyab Asghar
M Tayyab Asghar

Reputation: 322

Add button slider like image slider in kotlin

I am making a mobile app in kotlin in which I need to run different Machine Learning models by clicking buttons. The idea is simple there will only be one button at a time on the screen and the user can slide left or right to get the next button just like an image slider where you can access the next or prev image by sliding.

This effect can be achieved in kotlin by Horizontal Scroll View but the problem is if we do an incomplete scroll there can be 2 buttons on the screen but I want an autocomplete scroll effect where when you scroll only the next button should stay on screen.

Edited

So far I have used RecyclerView to implement my buttons but I can't seem to have a good startSmoothScroll. I embedded startSmoothScroll in onScrolled to trigger it for first time and to find the position to smoothScroll but it starts to jiggle between 2 items to and fro.

binding.recyclerView.setHasFixedSize(true)
    
val smoothScroller: SmoothScroller =
            object : LinearSmoothScroller(binding.recyclerView.context) {
                    override fun getHorizontalSnapPreference(): Int {
                    return SNAP_TO_START
                }
            }

binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                if (!(recyclerView.layoutManager as LinearLayoutManager).isSmoothScrolling) {
                    val firstPosition =
                        (recyclerView.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
                    val lastPosition =
                        (recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()

                    if ((targetPosition == 0) || (targetPosition < lastPosition))
                        targetPosition = lastPosition
                    else if (firstPosition != lastPosition)
                        targetPosition = firstPosition

                    smoothScroller.targetPosition = targetPosition

                    (recyclerView.layoutManager as LinearLayoutManager).startSmoothScroll(
                        smoothScroller
                    )
                }
            }
        })

Upvotes: 0

Views: 607

Answers (1)

Filip Petrovski
Filip Petrovski

Reputation: 444

Without having much code to go by, here's a general idea you could implement.

First try to set a setOnScrollChangeListener on your Horizontal Scroll View and override the onScrollChange. Depending on the direction, calculated by the current X and old X position, you can figure out the direction and appropriately move the view left or right: yourHorizontalScrollView.pageScroll(View.FOCUS_LEFT) //or View.FOCUS_RIGHT

If you use a RecyclerView, you could have a few more options with it's OnScrollListener and the recyclerView's smoothScrollToPosition()

Upvotes: 1

Related Questions