Material Design系列

Android(Lollipop/5.0)Material Design(一) 简单介绍

Android(Lollipop/5.0)Material Design(二) 入门指南

Android(Lollipop/5.0)Material Design(三) 使用Material主题

Android(Lollipop/5.0)Material Design(四) 创建列表和卡片

Android(Lollipop/5.0)Material Design(五) 定义阴影和裁剪View

Android(Lollipop/5.0)Material Design(六) 使用图片

Android(Lollipop/5.0)Material Design(七) 自己定义动画

Android(Lollipop/5.0)Material Design(八) 保持兼容性

官网:https://developer.android.com/training/material/lists-cards.html

在你的应用程序,创建复杂的列表和卡片与材料设计风格。您能够使用RecyclerView和CardView部件。

创建列表

RecyclerView组件是一个更先进和灵活的版本号的列表视图。

这个小部件是一个很有效率的容器,通过有限的views。能够滚动显示大型数据集。

RecyclerView组件数据集合的元素,可在执行时依据用户操作或网络事件进行改变。

RecyclerView类简化了显示和处理大型数据集,它提供了:

· 布局管理器

· 常见的默认动画item操作,如删除、加入项目

你能够在RecyclerView中灵活定义 布局管理器和动画

要使用RecyclerView组件,您必须指定一个适配器和布局管理器。创建一个适配器,继承RecyclerView.Adapter类。有关很多其它信息,请參见以下的样例。

RecyclerView并确定重用项目视图时,布局管理器的利用item的方法。不再是对用户可见。重用(或回收)视图,布局管理器可能会问适配器,替换内容为不同的数据集的元素。

回收view时,以这样的方式来改进性能:避免创建不必要的view或运行消耗大的findViewById()查询。

RecyclerView提供了例如以下管理器:

· LinearLayoutManager  横向或纵向的滚动列表

· GridLayoutManager  网格列表

· StaggeredGridLayoutManager  交错的网格列表

要创建一个自己定义布局管理器,须要继承RecyclerView.LayoutManager类

动画

加入和删除item的动画。在RecyclerView默认启用。定制这些动画,须要继承RecyclerView.ItemAnimator类并使用RecyclerView.setItemAnimator()方法。


样例

layout
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

activity

public class MyActivity extends Activity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true); //使用固定size 以优化性能 // use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager); // specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
...
}

adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset; // Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
} // Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
} // Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView)LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
// set the view's size, margins, paddings and layout parameters
...
ViewHolder vh = new ViewHolder(v);
return vh;
} // Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]); } // Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}

创建卡片

CardView继承自FrameLayout,以卡片式显示一致的外观。它能够有阴影和圆角
创建一个有阴影的卡片,使用card_view:cardElevation属性。

使用这些属性来定制CardView组件的外观:
· 在你的布局设置圆角半径,使用card_view:cardCornerRadius属性
· 在代码中设置圆角半径,使用CardView.setRadius方法
· 设置卡片的背景颜色,使用card_view:cardBackgroundColor属性
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
... >
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp"> <TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
</LinearLayout>

加入依赖

gradle依赖
dependencies {
...
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
}


Android(Lollipop/5.0) Material Design(四) 创建列表和卡片的更多相关文章

  1. Android(Lollipop/5.0) Material Design(六) 使用图像

    Material Design列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design(二 ...

  2. Android(Lollipop/5.0) Material Design(二) 入门指南

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简介 Android(Lollipop/5.0)Material Design(二) ...

  3. Android(Lollipop/5.0) Material Design(一) 简单介绍

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...

  4. Android(Lollipop/5.0) Material Design(一) 简介

    官网地址:https://developer.android.com/intl/zh-tw/design/material/index.html 使用Material Design 需要api21,即 ...

  5. Android(Lollipop/5.0) Material Design(六) 自定义动画

    官网地址:https://developer.android.com/intl/zh-tw/training/material/animations.html 动画在Material设计中,为用户与a ...

  6. Android最佳实践之Material Design

    Material概述及主题 学习地址:http://developer.android.com/training/material/get-started.html 使用material design ...

  7. Creating Lists and Cards 创建列表和卡片

    To create complex lists and cards with material design styles in your apps, you can use the Recycler ...

  8. [转]Android 5.0——Material Design详解(动画篇)

    Material Design:Google推出的一个全新的设计语言,它的特点就是拟物扁平化. Material Design包含了很多内容,今天跟大家分享一下Material新增的动画: 在Andr ...

  9. Android Lollipop 5.0 经典新特性回顾

    *Tamic 专注移动开发! 更多文章请关注 http://blog.csdn.net/sk719887916 虽然Android已到了7.0 ,但是我们还是不能忘怀视觉革命性改变的5.0,今天回顾下 ...

随机推荐

  1. pix格式的摸索(二)

    作者:朱金灿 来源:http://blog.csdn.net/clever101 PCI的系统格式pix是一个设计很巧妙的遥感图像格式,而且其设计巧妙之处不止一处两处,这些都有待我日后一一去摸索.今天 ...

  2. BZOJ 高精度开根 JAVA代码

    晓华所在的工作组正在编写一套高精度科学计算的软件,一些简单的部分如高精度加减法.乘除法早已写完了,现在就剩下晓华所负责的部分:实数的高精度开m次根.因为一个有理数开根之后可能得到一个无理数,所以这项工 ...

  3. day 5 集合

    # -*- coding: utf_8 _*_# Author:Vi#集合是无序的 list_1 = [1,2,3,2,3,5,7]list_1 = set(list_1)#将列表转变成集合list_ ...

  4. 【UML】UML在软件开发各个阶段的应用

    一.UML5个互联视图 UML中经常使用5个互联的视图来描写叙述系统的体系结构. 如图 (1)用例视图(Use-case View) 由专门描写叙述可被终于用户.分析人员.測试人员看到的系统行为的用例 ...

  5. git 工具的使用总结(5)-查看历史记录

    1.查看历史记录git log 1)不加参数,显示的就是节点号,作者,日期,注释 commit b7b310d220628530d1feb9e8046ccb59039d59f2 Author: zha ...

  6. 洛谷P1043 数字游戏

    题目描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分 ...

  7. THC=TERMINAL HANDLING CHARGE,碼頭操作費

    THC=TERMINAL HANDLING CHARGE,碼頭操作費

  8. MFC 任务托盘经常消失问题

    经常发现自己写的程序任务托盘会无缘无故的消失,但是进程还是存在的,原来是资源管理器异常的时候,重新生成的时候,程序需要重新添加下任务托盘. 当explorer进程重启,taskbar将会被创建,tas ...

  9. bootstrap课程7 jquery中结束之前动画用什么

    bootstrap课程7 jquery中结束之前动画用什么 一.总结 一句话总结:stop()方法.$('.navs').not($('.navs').eq(idx)).stop().hide(100 ...

  10. 有关Canvas的一点小事—图像绘制

    1.  使用canvas绘制图像 什么是图像?在js中它就是一个<img src=””>,<img>有两种接收图像信息的方法,一个是直接链接到图像地址,一个使用base64数据 ...