Android RecyclerView TV-滑动选中态背景

TV应用开发中,正常使用的是根据点击、焦点、selected换图片背景资源。但是效果看起来不怎么丝滑。

优化方案:
1、滑动过程中判断居中
2、选中态为滑动的。体验起来丝滑一些。
代码实现:

  private static final int ITEM_MOVE_TIME = 600;
    private static final int ANIM_VALUE = 60;

    private Paint mPaint;

    //四个目标坐标
    private int aimLeft;
    private int aimTop;
    private int aimBottom;
    private int aimRight;

    //目前的坐标
    private static int currLeft;
    private static int currTop;
    private int currBottom;
    private int currRight;

	//宽高
    private int width = 270;
    private int height  = 96;

    private RectF mRect;

    private ValueAnimator valueAnimator;

    public FocusBuoyView(Context context) {
        super(context);
    }

    public FocusBuoyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FocusBuoyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public FocusBuoyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        init();
        mRect.set(currLeft, currTop, currLeft + width, currTop + height);
        //画圆角矩形
        canvas.drawRoundRect(mRect, 14 , 14, mPaint);
    }

    private void init(){
        if (mPaint == null){
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.parseColor("#DC6666"));
            mPaint.setStyle(Paint.Style.FILL);
        }
        if (mRect == null){
            mRect = new RectF();
        }
    }

    public void setBuoy(int left, int top , int right, int bottom){
        this.aimLeft = left;
        this.aimTop = top;
        this.aimRight = right;
        this.aimBottom = bottom;

        if (valueAnimator == null){
            valueAnimator = new ValueAnimator();
            valueAnimator.setIntValues(0, ANIM_VALUE);
        }
        valueAnimator.removeAllUpdateListeners();
        valueAnimator.cancel();
        valueAnimator.setDuration(ITEM_MOVE_TIME);
        valueAnimator.setTarget(null);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int index = (int) animation.getAnimatedValue();
                currLeft = currLeft + (int)((aimLeft - currLeft) * (double)((double)index / (double)ANIM_VALUE));
                currTop = currTop + (int)((aimTop - currTop) * (double)((double) index / (double)ANIM_VALUE ));
                invalidate();
            }
        });
        valueAnimator.start();
    }

使用:

  homeRVAdapter.setOnFocusClickInterface(new BaseCommonListRVAdapter.OnFocusClick() {
            @Override
            public void onFocus(View view, int position, Object object, boolean focused, boolean parentFocus) {
				//设置坐标、刷新
                focusView.setBuoy(view.getLeft() + firstRV.getLeft(),
                        view.getTop() + firstRV.getTop(),
                        view.getRight()+ firstRV.getRight(),
                        view.getBottom()+ firstRV.getBottom());
            }
        });

优化:
发现长按的时候又拖拽的感觉:
替代方案:
使用Handler处理绘制,不用valueAnimator了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android RecyclerView 实现滑动悬停是通过使用 ItemDecoration 来实现的。在 RecyclerView滑动悬停需要满足两个条件:一是要有一个能够进行悬停的 View,二是要能够动地根据 RecyclerView 的滚动来改变悬停 View 的位置。 实现滑动悬停的步骤如下: 1. 首先,我们需要创建一个继承自 RecyclerView.ItemDecoration 的类,例如名为 StickyHeaderDecoration 的类,来实现悬停 View。 2. 在 StickyHeaderDecoration 类中,我们需要重写 getItemOffsets() 方法,该方法会在每次绘制 RecyclerView 的子项时被调用。在该方法中,我们可以根据子项的位置和需要悬停的条件来判断是否需要为该子项添加偏移量,从而实现悬停的效果。 3. 接下来,在 RecyclerView 的 Adapter 类中,我们需要重写两个方法:getItemViewType() 和 onCreateHolder()。 4. 在 getItemViewType() 方法中,我们可以根据当前子项的位置来判断是否需要为该子项设置一种特殊的 ViewType,用于标识悬停 View。例如当子项为要悬停的位置时,我们可以返回一个特定的 ViewType 值。 5. 在 onCreateHolder() 方法中,我们需要根据 ViewType 的不同创建不同的 ViewHolder。例如当 ViewType 为悬停 View 的类型时,我们可以创建一个单独的 ViewHolder 类来确定悬停 View 的样式。 6. 最后,在 RecyclerView 的布局文件中,我们需要将 StickyHeaderDecoration 添加到 RecyclerView 中,并设置它的布局参数。 通过以上步骤,我们就可以实现滑动悬停的效果了。当滚动 RecyclerView 时,悬停 View 会根据 RecyclerView 的滚动位置动地改变自己的位置,从而实现滑动悬停的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值