TV 纵向焦点居中ScrollView

2023-05-16

适用场景:

纵向,超过1屏,简单但样式不同的单选或多选按钮

例如一个TV设置页面里有几个已知的可选择项纵向排列,需要获取焦点的view一直在屏幕中间,VerticalGridView当然也可以。

用NestedScrollView简单的放几个view进去,然后加点击事件,处理一下焦点居中岂不是更方便。这样就直接往里塞view就可以了。

import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import androidx.core.widget.NestedScrollView
//import com.blankj.utilcode.util.LogUtils

/**
 * 焦点一直居中的纵向scrollview
 */
class TVFocusCenterScrollView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
    NestedScrollView(context, attrs, defStyleAttr) {
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, -1)
    constructor(context: Context) : this(context, null)

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        if (event?.action == KeyEvent.ACTION_DOWN) {
            when (event.keyCode) {

                KeyEvent.KEYCODE_DPAD_DOWN -> {
                    //获取当前焦点的横纵坐标
                    val currentFocus: View = findFocus()
                    val next = currentFocus.focusSearch(FOCUS_DOWN)
                    next?.let { it ->
                        val location = IntArray(2)
                        it.getLocationInWindow(location)
                        //如果已经超过半屏
                       // LogUtils.e("----d2----" + location[1] + "  " + this.height / 2)
                        if ((location[1] + it.height / 2) > this.height / 2) {
                            val needscroll = location[1] + it.height / 2 - this.height / 2
                          //  LogUtils.e("--------d3------" + needscroll)
                            scrollBy(0, needscroll)
                        }
                        val nextnext = it.focusSearch(FOCUS_DOWN)
                        if (nextnext == null) {
                            scrollTo(0, this.height)
                        }
                    }
                }

                KeyEvent.KEYCODE_DPAD_UP -> {
                    //获取当前焦点的横纵坐标
                    val currentFocus: View = findFocus()
                    val next = currentFocus.focusSearch(FOCUS_UP)

                    next?.let { it ->
                        val location = IntArray(2)
                        it.getLocationInWindow(location)
                        //如果已经超过半屏
                    //    LogUtils.e("----u1----" + location[1] )

                      //  LogUtils.e("----u2----"  + ( location[1] + it.height / 2) + "  " + this.height / 2)
                        if ( location[1] + it.height / 2 < this.height / 2) {
                            val needscroll =  location[1] - this.height / 2
                        //    LogUtils.e("--------u3------" + needscroll)
                            scrollBy(0, needscroll)
                        }
                        val nextnext = it.focusSearch(FOCUS_UP)
                        if (nextnext == null) {
                            scrollTo(0, 0)
                        }
                    }

                }
            }
        }
        return super.dispatchKeyEvent(event)
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TV 纵向焦点居中ScrollView 的相关文章

随机推荐