Creating Lists and Cards //创建列表和卡片

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

//创建材料设计风刮的灵活的列表和卡片,你可以使用RecyclerView 和CardView控件。

Create Lists //创建列表


The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events.

//这个RecyclerView控件比ListView控件更高级更灵活。这个控件是一个展示大数据能维持有限数量的视图高效滚动的容器。使用RecyclerView控件在当你有元素在用户操作或网络事件发生改变时的数据集合。

The RecyclerView class simplifies the display and handling of large data sets by providing:

//RecyclerView很简单展示和处理大数据:

  • Layout managers for positioning items //Item使用布局管理器管理
  • Default animations for common item operations, such as removal or addition of items //默认常见的item操作动画,比如增加或删除item。

You also have the flexibility to define custom layout managers and animations for RecyclerView widgets.

//你也可以灵活的自定义布局管理和动画。

Figure 1. The RecyclerView widget.

To use the RecyclerView widget, you have to specify an adapter and a layout manager. To create an adapter, extend the RecyclerView.Adapter class. The details of the implementation depend on the specifics of your dataset and the type of views. For more information, see the examples below.

//使用RecyclerView控件,你必须定义一个适配器和布局管理器。创建一个继承 RecyclerView.Adapter 的适配器。详细的实现需要根据你特定的数据和视图类型。

Figure 2 - Lists with RecyclerView.

A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible to the user. To reuse (or recycle) a view, a layout manager may ask the adapter to replace the contents of the view with a different element from the dataset. Recycling views in this manner improves performance by avoiding the creation of unnecessary views or performing expensive findViewById() lookups.

//布局管理器定位Item在RecyclerView里 和决定当用户不可见ItemView何时重用。对于重用(回收)一个视图,布局管理器将通知适配器把ItemView的内容替换掉数据源中不同的元素。习惯性得去回收视图可以提高性能,避免创建不必要的视图和使用高代价的 findViewById() 方法。

RecyclerView provides these built-in layout managers:

//RecyclerView提供默认的布局管理器:

To create a custom layout manager, extend the RecyclerView.LayoutManager class.//创建自定义的布局管理器,继承RecyclerView.LayoutManager

Animations

Animations for adding and removing items are enabled by default in RecyclerView. To customize these animations, extend the RecyclerView.ItemAnimator class and use the RecyclerView.setItemAnimator() method.

//增加和删除item动画在RecyclerView默认开启了。需要自定义Item动画,继承RecyclerView.ItemAnimator类和RecyclerView.setItemAnimator() 方法。

Examples

The following code example demonstrates how to add the RecyclerView to a 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"/>

Once you have added a RecyclerView widget to your layout, obtain a handle to the object, connect it to a layout manager, and attach an adapter for the data to be displayed:

//一旦你在布局中添加了RecyclerView控件,取出他的对象,连接到这个控件设置布局管理器,为数据展示设置一个适配器:

public class MyActivity extends Activity{
    private RecyclerView mRecyclerView;
    privateR ecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;     @Override
    protectedvoid 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);         // use a linear layout manager
        mLayoutManager =newLinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);         // specify an adapter (see also next example)
        mAdapter =newMyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }
    ...
}

The adapter provides access to the items in your data set, creates views for items, and replaces the content of some of the views with new data items when the original item is no longer visible. The following code example shows a simple implementation for a data set that consists of an array of strings displayed using TextView widgets:

//这个适配器提供在你的数据集中访问Item,为Item创建View,当原Item不可见的一些View的内容替换成新的Item数据。下面的代码示例展示了一个简单的实现通过一个TextView为一个由一些字符串数组组成数据集:

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
  //提供一个引用为了一个视图为每个Item有复杂数据的
    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
        View v =LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.my_text_view, parent,false);
        // set the view's size, margins, paddings and layout parameters
        ...
        ViewHolder vh =newViewHolder(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;
    }
}

Figure 3. Card examples.

Create Cards


CardView extends the FrameLayout class and lets you show information inside cards that have a consistent look across the platform. CardView widgets can have shadows and rounded corners.

To create a card with a shadow, use the card_view:cardElevation attribute. CardView uses real elevation and dynamic shadows on Android 5.0 (API level 21) and above and falls back to a programmatic shadow implementation on earlier versions. For more information, see Maintaining Compatibility.

Use these properties to customize the appearance of the CardView widget:

  • To set the corner radius in your layouts, use the card_view:cardCornerRadius attribute.
  • To set the corner radius in your code, use the CardView.setRadius method.
  • To set the background color of a card, use the card_view:cardBackgroundColor attribute.

The following code example shows you how to include a CardView widget in your layout:

<LinearLayoutxmlns: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>

For more information, see the API reference for CardView.

Add Dependencies


The RecyclerView and CardView widgets are part of the v7 Support Libraries. To use these widgets in your project, add these Gradle dependencies to your app's module:

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

Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details and restrictions, see the Content License.

About Android  |  Legal  |  Support

Material Design系列第八篇——Creating Lists and Cards的更多相关文章

  1. Material Design系列第七篇——Maintaining Compatibility

    Maintaining Compatibility This lesson teaches you to Define Alternative Styles Provide Alternative L ...

  2. Material Design系列第六篇——Defining Custom Animations

    Defining Custom Animations //自定义动画 This lesson teaches you to //本节课知识点 Customize Touch Feedback //1. ...

  3. Material Design系列第五篇——Working with Drawables

    Working with Drawables This lesson teaches you to Tint Drawable Resources Extract Prominent Colors f ...

  4. Material Design系列第四篇——Defining Shadows and Clipping Views

    Defining Shadows and Clipping Views This lesson teaches you to Assign Elevation to Your Views Custom ...

  5. Material Design系列第三篇——Using the Material Theme

    Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...

  6. Android Material Design系列之主题样式介绍说明

    今天这篇文章应该算是Material Design系列的补充篇,因为这篇文章本来应该放到前面讲的,因为讲的是主题嘛,对于一些状态和颜色的介绍,因为我们一新建一个项目时,系统自带了三个属性的颜色,现在就 ...

  7. Material Design系列第一篇——Creating Apps with Material Design

    Creating Apps with Material Design //创建Material Design的App Material design is a comprehensive guide ...

  8. Material Design系列第二篇——Getting Started

    Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elev ...

  9. Android Material Design 系列之 SnackBar详解

    SnackBar是google Material Design提供的一种轻量级反馈组件.支持从布局的底部显示一个简洁的提示信息,支持手动滑动取消操作,同时在同一个时间内只能显示一个SnackBar. ...

随机推荐

  1. Unity 各个组件参数总结

    今天在蛮牛教育上学习了NGUI的课程下面给大家总结了一些小知识点希望对大家有帮助UICamera-可以添加到任何相机,包含事件系统.UICamera是每个UI的重要组成部分.它负责发送Camera中所 ...

  2. 制作移动端手机网站过程中的SEO优化方法技巧

    据国内三大运营商数据来看,中国的手机用户数已达10亿,超过2/5的移动用户每个月都会从手机终端访问网页,如今的移动端手机网站比例肯定有提升,但是对于这些存在的移动版本网站来说,马海祥查看了很大一部分手 ...

  3. 【转】Asp.net MVC 通过自定义ControllerFactory实现构造器注入(重写DefaultControllerFactory)

    [转]Asp.net MVC 通过自定义ControllerFactory实现构造器注入 一.重写ControllerFactory的GetControllerInstance ControllerF ...

  4. logstash 主题综合篇

    一.[logstash-input-file]插件使用详解(配置) logstash input 监听多个目标文件. 二.Logstash Reference(官方参数配置说明)

  5. Java设计模式之五大创建型模式(附实例和详解)

    一.概况 总体来说设计模式分为三大类: (1)创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. (2)结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥 ...

  6. Logback中文文档(二):体系结构

    logback Logback 的基本结构充分通用,可应用于各种不同环境.目前,logback 分为三个模块:Core.Classic 和 Access. Core模块是其他两个模块的基础.Class ...

  7. FormData 对象的使用

    FormData 对象的使用 在本文章中 如何创建一个FormData对象 通过HTML表单创建FormData对象 使用FormData对象上传文件 通过AJAX提交表单和上传文件可以不使用Form ...

  8. python中,获取字符串的长度

    说明: 与其他的语言一样,有时候需要查看或者说计算字符串的长度.在此记录下python中通过哪个函数实现. 操作过程: 1.通过len()函数返回字符串的长度 >>> text='p ...

  9. mysql中查看视图的元数据?

    需求描述: 查看视图的元数据的方法. 操作过程: 1.通过查看information_schema数据库下的views表来查看视图的定义语句 mysql> select definer,view ...

  10. linux中显示有颜色的字符

    https://blog.csdn.net/weixin_42867972/article/details/81700469 # 设置命令提示符 PS1="\033[33;1m[\033[3 ...