1.RecyclerView
<1>知识点介绍
RecyclerView 比 ListView 更高级且更具灵活性。 它是一个用于显示庞大数据集的容器,可通过保持有限数量的视图进行非常有效的滚动操作。 如果您有数据集合,其中的元素将因用户操作或网络事件而在运行时发生改变,请使用 RecyclerView 。
RecyclerView代表的意义是,我只管Recycler View,也就是说RecyclerView只管回收与复用View,其他的你可以自己去设置。可以看出其高度的解耦,给予你充分的定制自由(所以你才可以轻松的通过这个控件实现ListView,GirdView,瀑布流等效果)
要实现一个RecyclerView,会接触到它的几个小伙伴,其中1、2是必须的。剩下的3、4、5三项,可以让RecyclerView更好看、效果更好。
(1)想要控制其item们的排列方式,请使用布局管理器LayoutManager
(2)如果要创建一个适配器,请使用RecyclerView.Adapter
(3)想要控制Item间的间隔,请使用RecyclerView.ItemDecoration
(4)想要控制Item增删的动画,请使用RecyclerView.ItemAnimator
(5)CardView 扩展 FrameLayout 类并让您能够显示卡片内的信息,这些信息在整个平台中拥有一致的呈现方式。CardView 小部件可拥有阴影和圆角。
2. 实际案例
<1>先将RecyclerView拖入界面
<2>源码(对应工程名为test17)
使用RecyclerView需在build.gradle中添加声明
implementation 'com.android.support:design:28.0.0'
(1)XML页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".recyclerview.Recycler2Activity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="添加" /> <Button android:id="@+id/button_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="删除" /> <Button android:id="@+id/button_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="list" /> <Button android:id="@+id/button_grid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="grid" /> <Button android:id="@+id/button_flow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="flow" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_test1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
(2)适配器源码
package com.lucky.test17.recyclerview; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import