在Android与recyclerview中使用列表和网格

1。Android和列表或网格

1.1。在Android中使用列表或网格

原文博客地址:http://www.apkbus.com/blog-920677-76896.htmlhtml

在列表或网格元素的显示是移动应用的一个很常见的模式。 用户看到的项目的集合,能够经过滚动的集合。 项目能够是一个列表,网格或另外一种结构化的数据表示。 等活动所描绘的是一个。java

用户经过触摸事件或工具栏项的集合的相互做用。 个别项目能够选择。 这个选择可能更新工具栏或屏幕详细基于触发的选择。 如下。android

1.2。使用recyclerview

这个recyclerview类支持一系列的数据显示。编程

这是一个现代版的ListViewGridView控件Android框架提供的类。 回收观问题,现有部件的地址有几个。 它执行的编程风格,产生了良好的性能。 它默认动画去也。app

recyclerview容许使用不一样的布局管理器定位项目。框架

回收视图使用viewholder存储引用的观点来看,在回收一个条目。 viewholder类在适配器持有相关观点引用静态内部类。 这些引用你的代码能够避免findviewbyid()用新的数据更新的部件的方法。函数

1.3。适配器

一个适配器管理数据模型和适应个别条目 扩展部件。recyclerview。适配器阶级和被分配到回收的观点经过recyclerview.setadapter方法。输入适配器一个回收的 视图能够是任意的java对象。 在此输入适配器总数必须归还物品getitemcount()方法工具

适配器准备项目的布局,为每一个单独的数据元素充气正确的布局。 这工做了oncreateviewholder方法 它返回一个对象。viewholder每一个视觉进入回收期。布局

这个实例是用于访问在布局视图。 oncreateviewholder方法只叫新视图必须建立。性能

在一个回收查看每个可见的入口充满了正确的数据模型项的适配器。 一旦数据项变得可见,适配器将此数据个别部件他膨胀的早期工做。 这onbindviewholder方法

例如,在一个列表中的条目可能会在左侧,两行文字中,以下面的图形描述的图像。

一个这样的线可能看起来像下面的布局文件。

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:maxLines="1"
        android:text="Description"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />

</RelativeLayout>

1.4。Gradle依赖使用回收的观点

 

这个recyclerview部件交付做为图书馆能够做为API 7级或更高。 添加一个依赖最新版本库到你的摇篮,创建文件使用。

 

dependencies {
    ...
    compile "com.android.support:recyclerview-v7:25.3.1"
}

 

1.5。默认的布局管理器

布局管理器决定如何在数据recyclerview显示。 回收查看图书馆提供下列建设布局。

  • linearlayoutmanager显示垂直滚动列表或水平的项目。

  • gridlayoutmanager显示在一个网格项目。

  • staggeredgridlayoutmanager显示在一个网格项目。

1.6。有关实现类的使用recyclerview

实施recyclerview须要几类来实现。 类最重要的列在下面

表1。重要的recyclerview API
目的 可选

适配器

提供的数据,负责建立的我的参赛的意见

要求的

viewholder

包含全部的视图的输入数据填充的参考

要求的

布局管理器

包含全部的视图的输入数据填充的参考

须要,可是默认的实现

itemdecoration

负责绘制装饰在顶部或入口视图容器

默认的行为,但能够被重写

itemanimator

负责若是添加条目定义动画,删除或从新排序

默认的行为,但能够被重写

你也能够为布局管理器和动画提供自定义实现。

1.7。在回收处理单击事件的见解

触摸事件,如点击应该由回收的意见处理。 若是视图应该引起一些对象在使用它(活动或片断),你能够经过构造函数的适配器经过它。 这容许适配器存储对象的引用和调用它的方法。

 

1.8。在回收视图布局

适配器须要为每一个条目提供的视图层次结构。 典型完成充气的XML布局。

 

 

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight">
        <!-- views contained in each line -->
</LinearLayout>

 

 

这根的布局是一个典型的ViewGroup(布局管理器)和包含几个其余的观点。 下图显示列表布局为奇数和偶数的不一样了。

getitemviewtype方法的回收期肯定哪些类型应该用于数据 框架自动调用。oncreateviewholder若是这种类型的须要。 这个方法你布置充气正确的类型,并返回一个拟合的观点。

1.9。自定义动画

在“自定义动画recyclerview,实现本身的动画的扩展recyclerview.itemanimator类并使用recyclerview。setitemanimator()方法为它指定 你的插件。

1.10。过滤和排序

过滤和排序是经过适配器来处理。 你须要逻辑在自定义适配器来实现。

1.11。在适配器的数据更新

这个notifyiteminserted(位置)在适配器的方法能够用来通知认为,一个新条目已插在某个位置。

这个notifyitemremoved(位置)方法可用于通知认为条目已在某个位置删除。

1.12。对于recyclerview刷卡支持

这个itemtouchhelper类使刷卡解雇和拖放简单实现。 实施ONMOVE拖放的方法和onswiped对核工业的支持。

看到[ recyclerview_swipe ]一个实例的实现。

 

2。练习:使用一个新的Android应用程序recyclerview

在这个练习中你建立了一个项目使用recyclerview类显示一个列表。

2.1。建立项目并添加工具的依赖

建立一个新的Android项目使用com.vogella.android.recyclerview顶层包名。

添加如下依赖你的摇篮,构建文件。

 

dependencies {
    ...
    compile "com.android.support:recyclerview-v7:25.3.1"
}

 

 

2.2。建立布局文件

 

建立或更新配置文件称为activity_main.xml因此,它包含recyclerview

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- A RecyclerView with some commonly used attributes -->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="12dp"
        android:layout_marginRight="12dp"
        android:elevation="2dp"
        android:src="@android:drawable/ic_menu_add" />

</RelativeLayout>
相关文章
相关标签/搜索