In this tutorial, we’ll learn how to create animation for a RecyclerView in which elements are displayed in the form of a Grid.

在本教程中,我们将学习如何为RecyclerView创建动画,其中的元素以Grid形式显示。

We’ve already discussed RecyclerView Layout Animations earlier using a List.

我们之前已经使用列表讨论了RecyclerView布局动画 。

RecyclerView网格动画 (RecyclerView Grid Animation)

In the previous tutorial, we created List based Layout Animations. At the end of the tutorial, we applied them to a Grid Layout and saw that the animation is still list based on the grid instead of row and column based.

在上一教程中,我们创建了基于列表的布局动画。 在本教程的最后,我们将它们应用于“网格布局”,发现动画仍然是基于网格的列表,而不是基于行和列的列表。

In order to show Grid-based animation we need to use <gridlayoutanimation tag.
But this won’t work on a normal RecyclerView. It will crash since by default a RecyclerView doesn’t know anything about how the elements are placed by the Layout Manager and assumes a list based placement.

为了显示基于网格的动画,我们需要使用<gridlayoutanimation标签。
但这在正常的RecyclerView上不起作用。 它将崩溃,因为默认情况下,RecyclerView对布局管理器如何放置元素一无所知,并假定基于列表的放置方式。

Hence we need to create a custom RecyclerView first, overriding the important methods.

因此,我们需要首先创建一个自定义的RecyclerView,以覆盖重要的方法。

Let’s get started with our application.

让我们开始使用我们的应用程序。

项目结构 (Project Structure)

码 (Code)

The code for the CustomGridRecyclerView.java class is given below:

下面给出CustomGridRecyclerView.java类的代码:

package com.journaldev.androidrecyclerviewgridlayoutanimations;import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.GridLayoutAnimationController;public class CustomGridRecyclerView extends RecyclerView {public CustomGridRecyclerView(Context context) {super(context);}public CustomGridRecyclerView(Context context, AttributeSet attrs) {super(context, attrs);}public CustomGridRecyclerView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic void setLayoutManager(LayoutManager layout) {if (layout instanceof GridLayoutManager) {super.setLayoutManager(layout);} else {throw new ClassCastException("This recyclerview should use grid layout manager as the layout manager");}}@Overrideprotected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count) {if (getAdapter() != null && getLayoutManager() instanceof GridLayoutManager) {GridLayoutAnimationController.AnimationParameters animationParams =(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;if (animationParams == null) {animationParams = new GridLayoutAnimationController.AnimationParameters();params.layoutAnimationParameters = animationParams;}int columns = ((GridLayoutManager) getLayoutManager()).getSpanCount();animationParams.count = count;animationParams.index = index;animationParams.columnsCount = columns;animationParams.rowsCount = count / columns;final int invertedIndex = count - 1 - index;animationParams.column = columns - 1 - (invertedIndex % columns);animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;} else {super.attachLayoutAnimationParameters(child, params, index, count);}}
}

In the above code, we’ve explicitly set the RecyclerView to use grids row and columns for animating.

在上面的代码中,我们明确设置了RecyclerView以使用网格行和列进行动画处理。

The code for the activity_main.xml is given below:

下面给出了activity_main.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:app="https://schemas.android.com/apk/res-auto"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.journaldev.androidrecyclerviewgridlayoutanimations.CustomGridRecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="8dp"android:src="@android:drawable/ic_media_next"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toRightOf="parent" /></android.support.constraint.ConstraintLayout>

The code for the MainActivity.java is given below:

MainActivity.java的代码如下:

package com.journaldev.androidrecyclerviewgridlayoutanimations;import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.GridLayout;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {CustomGridRecyclerView recyclerView;RecyclerViewAdapter recyclerViewAdapter;FloatingActionButton fab;ArrayList<String> arrayList = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fab = findViewById(R.id.fab);recyclerView = findViewById(R.id.recyclerView);populateData();initAdapter();fab.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {runAnimationAgain();}});}private void populateData() {for (int i = 0; i < 20; i++) {arrayList.add("Item " + i);}}private void initAdapter() {recyclerView.setLayoutManager(new GridLayoutManager(this, 3));recyclerViewAdapter = new RecyclerViewAdapter(arrayList);recyclerView.setAdapter(recyclerViewAdapter);}private void runAnimationAgain() {final LayoutAnimationController controller =AnimationUtils.loadLayoutAnimation(this, R.anim.gridlayout_animation_from_bottom);recyclerView.setLayoutAnimation(controller);recyclerViewAdapter.notifyDataSetChanged();recyclerView.scheduleLayoutAnimation();}
}

The animation set is defined in the anim folder in the file down_to_up.xml.

动画集在文件down_to_up.xmlanim文件夹中定义。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"android:duration="500"><translateandroid:fromYDelta="50%p"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:toYDelta="0" /><alphaandroid:fromAlpha="0"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:toAlpha="1" /></set>

The grid layout animation is defined in the file: gridlayout_animation_from_bottom.xml.

网格布局动画在以下文件中定义: gridlayout_animation_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<gridLayoutAnimation xmlns:android="https://schemas.android.com/apk/res/android"android:animation="@anim/down_to_up"android:animationOrder="normal"android:columnDelay="15%"android:direction="top_to_bottom|left_to_right"android:rowDelay="15%"android:startOffset="700" />

The row and column delay attributes are used to define the delay in animation in each row and column respectively after the previous row got animated.

行和列延迟属性用于在上一行获得动画之后分别定义每行和每列中动画的延迟。

The code for the RecyclerViewAdapter.java class is given below:

下面给出了RecyclerViewAdapter.java类的代码:

package com.journaldev.androidrecyclerviewgridlayoutanimations;import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import java.util.List;public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemViewHolder> {List<String> itemList;public RecyclerViewAdapter(List<String> itemList) {this.itemList = itemList;}@NonNull@Overridepublic ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_row, viewGroup, false);return new ItemViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull ItemViewHolder myViewHolder, int position) {myViewHolder.tvItem.setText(itemList.get(position));}@Overridepublic int getItemCount() {return itemList == null ? 0 : itemList.size();}public class ItemViewHolder extends RecyclerView.ViewHolder {TextView tvItem;public ItemViewHolder(@NonNull View itemView) {super(itemView);tvItem = itemView.findViewById(R.id.tvItem);}}
}

The output of the above application in action is given below:

上面应用程序的输出如下:

That brings an end to this tutorial. You can download the complete project from the link below:

这样就结束了本教程。 您可以从下面的链接下载完整的项目:

AndroidRecyclerViewGridLayoutAnimationsAndroidRecyclerViewGridLayoutAnimations
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/24127/android-recyclerview-grid-layout-animations

Android RecyclerView网格布局动画相关推荐

  1. android 分割线布局,Android RecyclerView网格布局(支持多种分割线)详解(2)

    记录了下RecyclerView的使用方法,并且讲述了线性布局列表的使用方法,在此基础上加上了万能分割线,支持颜色分割线和图片分割线,同时支持对分割线设置线宽. 这篇是总结一下网格布局的使用,同样也支 ...

  2. 相片堆叠瀑布流网格布局动画效果

    介绍: 一款效果超酷堆叠相片转瀑布流网格布局动画效果设计.该效果的灵感来源于takeit网站,它上面的一组堆叠相片在点击按钮或向下滚动鼠标时,会动画转换为网格瀑布流布局 网盘下载地址: http:// ...

  3. Android GridLayout网格布局实现复古小米计算器

    Android GridLayout网格布局实现复古小米计算器 闲暇时间整理一些Android基础知识 首先看效果图 首先是一些配色: <color name="btnColor&qu ...

  4. Android 图片网格布局控件

    Android 图片网格布局控件 项目地址:MultiPictureView MultiPictureView是一个可以将多张图片以网格的方式显示的View,通过简单的接口实现烦人的布局,从此解放你的 ...

  5. android网格布局间隙,RecyclerView网格布局瀑布流布局设置间距

    RecyclerView在网格布局或者瀑布流布局下,如果要设置间距,可以使用ItemDecoration. 下面的代码是设置显示两列数据RecyclerView的情况. cat.png @Overri ...

  6. RecyclerView网格布局

    GridLayoutManager 一. 引入问题 要实现这个布局,我们需要怎么做呢? 用线性布局画各种长度尺寸的button? 可行,但扩展性不够,假如将第二行和第三行互换位置呢 再增加几行呢 再增 ...

  7. android recyclerview多布局_图文讲解RecyclerView的复用机制 ||Recyclerview进阶

    码个蛋(codeegg) 第 1085 次推 作者:susion 博客地址:https://juejin.im/post/5c1369cff265da613b6fa87f 本文是RecyclerVie ...

  8. android功能网格布局,Android布局总结一:GridLayout布局(网格布局)

    GridLayout布局简介 GridLayout布局是Android4.0(API Level 14)新引入的网格矩阵形式的布局控件. GridLayout属性介绍 本身属性 android:ali ...

  9. Android RecyclerView复杂布局 实现多Item,item中含视频文件播放

    效果(随手画的啊,哈哈哈) 主要就是Adapter里面的内容 import android.content.Context; import android.content.SharedPreferen ...

最新文章

  1. 【每日一算法】罗马数字转整数
  2. 万众瞩目,2018中国企业数字化转型国际峰会,重磅来袭
  3. 两台服务器安装redis集群_Redis Cluster搭建高可用Redis服务器集群
  4. 只有程序员才有的十大烦恼
  5. netapp学习(五)---创建volume
  6. luogu P3244 [HNOI2015]落忆枫音
  7. 让MySQL速度提升3倍的19种优化方式
  8. SpringSecurity的认识和整合流程
  9. python pdf转html代码_python将html转成PDF的实现代码(包含中文)
  10. 解决 elementUI 切换table后 el_table 固定列下方多了一条线
  11. 时分多路复用(Time Division Multiplexing,TDM)
  12. 二进制编译安装mysql(centos6、7)和源码编译bind
  13. 检测移动设备(手机)的 PHP 类库
  14. Java:for循环出现for(int i : arr)
  15. idea分支切换注意事项
  16. R语言使用ltm包计算cronbach‘s alpha(克朗巴哈系数法)实战:cronbach alpha(克朗巴哈系数法)是一种测量问卷或调查内部一致性的方法、cronbach‘s alpha解读
  17. 电视机尺寸与观看距离
  18. 如何批量修改文件后缀名?(批量修改文件的扩展名)
  19. 文件上传服务器取直链,云服务器直链
  20. 有哪些好用的相机软件app?除了轻颜相机,还有两款可能你也认识

热门文章

  1. 向新手和不愿意尝试的童鞋推荐vim插件(转载)
  2. 考研数学一之高数上册学习计划
  3. [转载] Python3 日历(Calendar)模块介绍
  4. 机器学习-数据科学库-day5
  5. 不会框架不要紧,我带你自定义框架
  6. HTML-JS-CSS基础
  7. RabbitMQ消息队列入门篇(环境配置+Java实例+基础概念)
  8. 【Mac】Mac键盘实现Home, End, Page UP, Page DOWN
  9. Spring(三)Bean继续入门
  10. 已知一个函数f可以等概率的得到1-5间的随机数,问怎么等概率的得到1-7的随机数...