美文网首页
android tv常见问题(四)焦点变化时,Recyclerv

android tv常见问题(四)焦点变化时,Recyclerv

作者: Geekholt | 来源:发表于2019-12-25 20:04 被阅读0次

    如需转载请评论或简信,并注明出处,未经允许不得转载

    系列文章

    github地址

    https://github.com/Geekholt/TvFocus

    目录

    期望结果

    Recyclerview滚动时,聚焦的item位置保持在中间。


    4.1.gif

    实际结果

    4.2.gif

    问题分析

    需要在计算RecyclerView滑动距离的方法中进行重写,控制每次滑动的距离。先来看看RecyclerView原生的滑动距离计算方法。

    RecyclerView#requestChildRectangleOnScreen

    当RecyclerView的某个子View需要被定位在屏幕的某个矩形范围时,调用此方法。

        /**
         * 通过该方法设置选中的item居中
         * <p>
         * 最终计算出的dy,dx的实际意义就是在滚动中上下和左右滑动的距离
         *
         * @param child     发出请求的子View
         * @param rect      子View坐标系内的矩形,即此子View希望在屏幕上的定位
         * @param immediate 设为true,则禁止动画和平滑移动滚动条
         * @return 进行了滚动操作的这个ViewGroup,是否处理此操作
         */
    public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect,
            boolean immediate) {
        final int parentLeft = getPaddingLeft();
        final int parentTop = getPaddingTop();
        final int parentRight = getWidth() - getPaddingRight();
        final int parentBottom = getHeight() - getPaddingBottom();
        final int childLeft = child.getLeft() + rect.left - child.getScrollX();
        final int childTop = child.getTop() + rect.top - child.getScrollY();
        final int childRight = childLeft + rect.width();
        final int childBottom = childTop + rect.height();
    
        final int offScreenLeft = Math.min(0, childLeft - parentLeft);
        final int offScreenTop = Math.min(0, childTop - parentTop);
        final int offScreenRight = Math.max(0, childRight - parentRight);
        final int offScreenBottom = Math.max(0, childBottom - parentBottom);
    
        // Favor the "start" layout direction over the end when bringing one side or the other
        // of a large rect into view. If we decide to bring in end because start is already
        // visible, limit the scroll such that start won't go out of bounds.
        final int dx;
        if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
            dx = offScreenRight != 0 ? offScreenRight
                    : Math.max(offScreenLeft, childRight - parentRight);
        } else {
            dx = offScreenLeft != 0 ? offScreenLeft
                    : Math.min(childLeft - parentLeft, offScreenRight);
        }
    
        // Favor bringing the top into view over the bottom. If top is already visible and
        // we should scroll to make bottom visible, make sure top does not go out of bounds.
        final int dy = offScreenTop != 0 ? offScreenTop
                : Math.min(childTop - parentTop, offScreenBottom);
    
        if (dx != 0 || dy != 0) {
            if (immediate) {
                parent.scrollBy(dx, dy);
            } else {
                parent.smoothScrollBy(dx, dy);
            }
            return true;
        }
        return false;
    }
    

    解决方案

        public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
            //计算偏移量
            int selectedItemOffsetStart = 0;
            int selectedItemOffsetEnd = 0;
            selectedItemOffsetStart = !isVertical() ? (getFreeWidth() - child.getWidth()) : (getFreeHeight() - child.getHeight());
            selectedItemOffsetStart /= 2;
            selectedItemOffsetEnd = selectedItemOffsetStart;
            
            final int parentLeft = getPaddingLeft();
            final int parentTop = getPaddingTop();
            final int parentRight = getWidth() - getPaddingRight();
            final int parentBottom = getHeight() - getPaddingBottom();
            final int childLeft = child.getLeft() + rect.left - child.getScrollX();
            final int childTop = child.getTop() + rect.top - child.getScrollY();
            final int childRight = childLeft + rect.width();
            final int childBottom = childTop + rect.height();
    
    
            final int offScreenLeft = Math.min(0, childLeft - parentLeft - mSelectedItemOffsetStart);
            final int offScreenRight = Math.max(0, childRight - parentRight + mSelectedItemOffsetEnd);
    
            final int offScreenTop = Math.min(0, childTop - parentTop - mSelectedItemOffsetStart);
            final int offScreenBottom = Math.max(0, childBottom - parentBottom + mSelectedItemOffsetEnd);
    
            // Favor the "start" layout direction over the end when bringing one side or the other
            // of a large rect into view. If we decide to bring in end because start is already
            // visible, limit the scroll such that start won't go out of bounds.
            final int dx;
            if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
                dx = offScreenRight != 0 ? offScreenRight
                        : Math.max(offScreenLeft, childRight - parentRight);
            } else {
                dx = offScreenLeft != 0 ? offScreenLeft
                        : Math.min(childLeft - parentLeft, offScreenRight);
            }
    
            // Favor bringing the top into view over the bottom. If top is already visible and
            // we should scroll to make bottom visible, make sure top does not go out of bounds.
            final int dy = offScreenTop != 0 ? offScreenTop
                    : Math.min(childTop - parentTop, offScreenBottom);
    
            if (dx != 0 || dy != 0) {
                if (immediate) {
                    scrollBy(dx, dy);
                } else {
                    smoothScrollBy(dx, dy);
                }
                return true;
            }
            return false;
        }
    

    这里要注意的是,为了适配v7,需要自定义LayoutManager,不然RecyclerView的requestChildRectangleOnScreen可能无法执行。

    public class V7LinearLayoutManager extends LinearLayoutManager {
        public V7LinearLayoutManager(Context context) {
            super(context);
        }
    
        public V7LinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }
    
        public V7LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr, 0);
        }
    
        @Override
        public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {
            if(parent instanceof TvRecyclerView) {
                return parent.requestChildRectangleOnScreen(child, rect, immediate);
            }
            return super.requestChildRectangleOnScreen(parent, child, rect, immediate, focusedChildVisible);
        }
    }
    

    系列文章总结

    1. 想要改变焦点查找规则,可以关注focusSearch的过程。
    2. 想要监听焦点变化的回调,可以关注requestFocus的过程。

    如果想要实现一套通用焦点框架,个人想法是在Android原生焦点机制的基础上做一些定制化的操作,或许并不需要完全自己去实现一套焦点框架。

    TV端焦点问题的比较复杂的根本问题我认为有两点:

    1. 主观因素可能是我们对Android原生的焦点机制还没有特别的清楚,所以不知道如何下手去处理一些不符合预期的现象。其实这些现象如果跟着源码去看的话,会发现它的实现都是有一定道理的。
    2. 客观因素是某些的UI交互比较复杂,Andorid原生的焦点机制只是采用了比较折中的处理方案。没有什么语言是完美的,也没有什么框架是完美的,能满足我们需求才是最好的。所以我认为焦点问题的处理应该建立在我们有一套统一的UI交互的基础上,然后我们在去基于Android原生焦点机制做一些定制化的操作,具体如何定制化,基本上问题都可以在文中提到的几个回调接口中去处理。

    相关文章

      网友评论

          本文标题:android tv常见问题(四)焦点变化时,Recyclerv

          本文链接:https://www.haomeiwen.com/subject/xthhoctx.html