在RecyclerView中放大项目以重叠2个相邻项目Android

Huy*_* Tu 14 android android-recyclerview

我正在使用RecyclerView和GridLayoutManager.我想要实现的目标是当我点击一个项目时,它会向上扩展并重叠相邻的项目.如下图所示(在Android TV中) 在此输入图像描述

触发onClick事件时,我会调用

v.animate().scaleX(1.2f).scaleY(1.2f).setDuration(500).start();
Run Code Online (Sandbox Code Playgroud)

但结果如下: 在此输入图像描述

它只能重叠位置低于自身的项目.
我该怎么做才能重叠所有相邻的项目.提前致谢.
编辑
我已经尝试过:

v.bringToFront();
Run Code Online (Sandbox Code Playgroud)

要么

(v.getParent()).bringChildToFront(v);
Run Code Online (Sandbox Code Playgroud)

但是它们都不起作用.

Xia*_*zou 5

根据aga的回答,我使用ViewCompat.setElevation(View view,float height)来支持21之前的API,它的工作原理很迷人。

static class ViewHolder extends RecyclerView.ViewHolder {

  public ViewHolder(View root) {
    // bind views
    // ...

    // bind focus listener
    root.setOnFocusChangeListener(new View.OnFocusChangeListener() {
      @Override
        public void onFocusChange(View v, boolean hasFocus) {
          if (hasFocus) {
            // run scale animation and make it bigger

            ViewCompat.setElevation(root, 1);
          } else {
            // run scale animation and make it smaller

            ViewCompat.setElevation(root, 0);
          }
        }
     });
  }
}
Run Code Online (Sandbox Code Playgroud)

项目布局文件非常简单,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:background="@drawable/sl_live_item" />
Run Code Online (Sandbox Code Playgroud)


aga*_*aga 0

v.bringToFront()尝试在运行动画之前调用。
如果它不起作用,另一种变体是使用View#setElevation(float val)where val > 0。缺点是当您取消选择该项目时,需要将海拔设置回 0。