Android TV 中RecyclerView聚焦Item实现居中功能

Android TV 中RecyclerView聚焦Item实现居中功能,第1张

概述最近预研EPG一些界面功能的时候需要实现一种节目列表选中自动居中放大的功能,查找一些网上的资料,发现大多数是重写RecyclerView的布局管理器的smoothScrollToPosition函数,该方法通过修改滑动Scroller里面的偏移量来达到居中的效果,不过在测试的过程中我发现当按住遥控器一会再松

@H_403_3@@H_403_3@

最近预研EPG一些界面功能的时候需要实现一种节目列表选中自动居中放大的功能,查找一些网上的资料,发现大多数是重写RecyclerVIEw的布局管理器的smoothScrollToposition函数,该方法通过修改滑动Scroller里面的偏移量来达到居中的效果,不过在测试的过程中我发现当按住遥控器一会再松开时,列表会滚动一段时间再停止,无法实现即停的效果,界面 *** 作起来不是很友好。@H_403_3@

经过一些资料的搜索,发现可以在适配器里面做居中 *** 作,具体做法是Item添加一个聚焦的监听,然后对当前Item的坐标以及RecyclerVIEw的位置计算出需要偏移的长度,最后调用RecyclerVIEw的smoothScrollBy函数进行滚动。下面是实现步骤:@[email protected]修改子类绘制顺序

在RecyclerVIEw构造函数中添加以下代码,以允许子类修改绘制顺序@H_403_3@

setChildrenDrawingOrderEnabled(true);

重写getChildDrawingOrder函数,主要是修改子Item绘制顺序@H_403_3@

@OverrIDeprotected int getChildDrawingOrder(int childCount, int position) {    VIEw focusedVIEw = getFocusedChild();    if (null != focusedVIEw) {        int pos = indexOfChild(focusedVIEw);        /* 这是最后一个需要刷新的item */        if (position == childCount - 1) {            if (pos > position) {                pos = position;            }            return pos;        } else if (pos == position) {            /* 这是原本要在最后一个刷新的item */            return childCount - 1;        }    }    return position;}
2.在Adapter里面做一些聚焦滚动 *** 作

首先可以在VIEwHolder的构造函数对聚焦的Item设置聚焦监听@H_403_3@

itemVIEw.setonFocuschangelistener(new VIEw.OnFocuschangelistener() {                @OverrIDe                public voID onFocusChange(VIEw v, boolean hasFocus) {                    if (hasFocus) {                        int[] amount = getScrollAmount(rv, itemVIEw);//计算需要滑动的距离                        rv.smoothScrollBy(amount[0], amount[1]);    	  offloatAnimator(itemVIEw, 1f, 1.3f);                    }else{   offloatAnimator(itemVIEw, 1.3f, 1f);}                }            });/** * 计算需要滑动的距离,使焦点在滑动中始终居中 * * @param recyclerVIEw * @param vIEw */private static int[] getScrollAmount(RecyclerVIEw recyclerVIEw, VIEw vIEw) {    int[] out = new int[2];    int position = recyclerVIEw.getChildAdapterposition(vIEw);    final int parentleft = recyclerVIEw.getpaddingleft();    final int parenttop = recyclerVIEw.getpaddingtop();    final int parentRight = recyclerVIEw.getWIDth() - recyclerVIEw.getpaddingRight();    final int parentBottom = recyclerVIEw.getHeight() - recyclerVIEw.getpaddingBottom();    final int childleft = vIEw.getleft() - vIEw.getScrollX();    final int childtop = vIEw.gettop() - vIEw.getScrollY();    //item左边距减去RecyclervIEw不在屏幕内的部分,加当前RecyclervIEw一半的宽度就是居中    final int dx = childleft - parentleft - ((parentRight - vIEw.getWIDth()) / 2);    //同上    final int dy = childtop - parenttop - (parentBottom - vIEw.getHeight()) / 2;    out[0] = dx;    out[1] = dy;    return out;}/** * @param vIEw  作用的VIEw * @param start 开始大小 * @param end   结束大小 */private static voID offloatAnimator(VIEw vIEw, float start, float end) {    AnimatorSet animatorSet = new AnimatorSet();    animatorSet.setDuration(700);//动画时间    ObjectAnimator scaleX = ObjectAnimator.offloat(vIEw, "scaleX", start, end);    ObjectAnimator scaleY = ObjectAnimator.offloat(vIEw, "scaleY", start, end);    animatorSet.setInterpolator(new DecelerateInterpolator());//插值器    animatorSet.play(scaleX).with(scaleY);//组合动画,同时基于x和y轴放大    animatorSet.start();}
3.为RecyclerVIEw前后加入间隙,以便滚动到第一个或者最后一个可以居中

新建一个类继承RecyclerVIEw.Itemdecoration,重写getItemOffsets函数@H_403_3@

@OverrIDepublic voID getItemOffsets(@NonNull Rect outRect, @NonNull VIEw vIEw, @NonNull RecyclerVIEw parent, @NonNull RecyclerVIEw.State state) {    int position = parent.getChildAdapterposition(vIEw);    if (position > 0 && state.getItemCount() > 0) { //第一项左边或者顶部留间隙        if (orIEntation == RecyclerVIEw.HORIZONTAL) {            outRect.set(startGap, 0, 0, 0);        } else if (orIEntation == RecyclerVIEw.VERTICAL) {            outRect.set(0, startGap, 0, 0);        }    } else if (position == state.getItemCount() - 1) { //最后一项右边或者底部流间隙        if (orIEntation == RecyclerVIEw.HORIZONTAL) {            outRect.set(0, 0, endGap, 0);        } else if (orIEntation == RecyclerVIEw.VERTICAL) {            outRect.set(0, 0, 0, endGap);        }    } else {        outRect.set(0, 0, 0, 0);    }}

 @H_403_3@ 总结

以上是内存溢出为你收集整理的Android TV 中RecyclerView聚焦Item实现居中功能全部内容,希望文章能够帮你解决Android TV 中RecyclerView聚焦Item实现居中功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1056581.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存