使其与Android的2个相邻项目重叠

使其与Android的2个相邻项目重叠

本文介绍了在RecyclerView中按比例放大项目,使其与Android的2个相邻项目重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将RecyclerView与GridLayoutManager一起使用.我要实现的目标是单击一个项目,它会放大并与相邻项目重叠.就像下面的图片(在Android TV中)

I'm using RecyclerView with GridLayoutManager. The goal I want to achieve is when I click on an item, It'll scale up and overlaps the adjacent items. Just like the picture below (in Android TV)

触发onClick事件时,我会打电话

When the onClick event is triggered, I call

v.animate().scaleX(1.2f).scaleY(1.2f).setDuration(500).start();

但是结果如下:

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

It can overlaps only items that has position lower than itself.
What should I do to overlaps all of the adjacent items. Thanks in advance.
EDIT
I already tried:

v.bringToFront();

(v.getParent()).bringChildToFront(v);

但是他们两个都不起作用.

But both of them don't work.

推荐答案

根据 aga的答案,我使用 ViewCompat.setElevation(View view,float height)支持API在21岁之前,它就像一个迷人的东西.

According aga's answer, i use ViewCompat.setElevation(View view, float elevation) to support API prior to 21, It works like a charming.

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);
          }
        }
     });
  }
}

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

The item layout file is very simple like below:

<?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" />

这篇关于在RecyclerView中按比例放大项目,使其与Android的2个相邻项目重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:04