Search code examples
androidandroid-tvhorizontalscrollview

HorizontalScrollView programmatically scroll without focus


I develop Android TV app and have a custom view looks like HorizontalScrollView with TextView child. When I press some button, HorizontalScrollView must scroll long child text in left\right destination. But when i call

scrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT)

horizontalScrollView request focus on himself, so button loose focus coz only one view can be focused at the same time.

How to scroll HorizontalScrollView programmatically whithout requesting focus on it, in order to button does not lose focus?


Solution

  • I find some solution with ObjectAnimator. Just animate scrollX value.

    fun scrollTo(scrollDestination: ScrollDestinations) {
            val targetXScroll = when(scrollDestination) {
                ScrollDestinations.START -> 0
                ScrollDestinations.END -> mTextView.width
            }
    
            val animator = ObjectAnimator.ofInt(mScroll, "scrollX", targetXScroll)
            animator.apply {
                duration = 100
                start()
            }
        }
    

    In this snap mScroll is HorizontalScrollView with mTextView child. ScrollDestinations is just enum.

    But in my case it has some overscroll when i scroll in "right" destination, maybe something wrong with TextView width. In my case i can ignore this overscroll but maybe someone know where is problem.