热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MaterialDesign系列第八篇——CreatingListsandCards

CreatingListsandCards创建列表和卡片Tocreatecomplexlistsandcardswithmaterialdesignstylesinyourapps
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提供默认的布局管理器:

  • LinearLayoutManager shows items in a vertical or horizontal scrolling list. //线性布局管理器 展示垂直或水平的滑动列表
  • GridLayoutManager shows items in a grid. //网格布局管理器
  • StaggeredGridLayoutManager shows items in a staggered grid. //瀑布流布局管理器

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:



   
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
&#61; v;
       
}
   
}

   
// Provide a suitable constructor (depends on the kind of dataset)
   
public MyAdapter(String[] myDataset){
        mDataset
&#61; myDataset;
   
}

   
// Create new views (invoked by the layout manager)
   
&#64;Override
   
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   
int viewType){
       
// create a new view
       
View v &#61;LayoutInflater.from(parent.getContext())
                               
.inflate(R.layout.my_text_view, parent,false);
       
// set the view&#39;s size, margins, paddings and layout parameters
       
...
       
ViewHolder vh &#61;newViewHolder(v);
       
return vh;
   
}

   
// Replace the contents of a view (invoked by the layout manager)
   
&#64;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)
   
&#64;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:

xmlns:android&#61;"http://schemas.android.com/apk/res/android"
   
xmlns:tools&#61;"http://schemas.android.com/tools"
   
xmlns:card_view&#61;"http://schemas.android.com/apk/res-auto"
    ...
>
   

   

       
xmlns:card_view&#61;"http://schemas.android.com/apk/res-auto"
       
android:id&#61;"&#64;&#43;id/card_view"
       
android:layout_gravity&#61;"center"
       
android:layout_width&#61;"200dp"
       
android:layout_height&#61;"200dp"
       
card_view:cardCornerRadius&#61;"4dp">

       

           
android:id&#61;"&#64;&#43;id/info_text"
           
android:layout_width&#61;"match_parent"
           
android:layout_height&#61;"match_parent"/>
   

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&#39;s module:

dependencies {
   
...
    compile
&#39;com.android.support:cardview-v7:21.0.&#43;&#39;
    compile
&#39;com.android.support:recyclerview-v7:21.0.&#43;&#39;
}


转:https://www.cnblogs.com/bvin/p/4208699.html



推荐阅读
  • FFPlay 字幕与LRC歌词播放指南
    本文详细介绍了不同媒体容器支持的字幕格式,以及如何使用FFPlay和FFMPEG进行字幕和LRC歌词的播放与转换。涵盖的内容包括字幕显示方法、字体配置、字幕流选择等。 ... [详细]
  • 作为一名专业的Web前端工程师,掌握HTML和CSS的命名规范是至关重要的。良好的命名习惯不仅有助于提高代码的可读性和维护性,还能促进团队协作。本文将详细介绍Web前端开发中常用的HTML和CSS命名规范,并提供实用的建议。 ... [详细]
  • Android 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 本文详细介绍了W3C标准盒模型和IE传统盒模型的区别,探讨了CSS3中box-sizing属性的使用方法及其在布局中的重要性。通过实例分析,帮助读者更好地理解和应用这一关键概念。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 本文探讨了领域驱动设计(DDD)的核心概念、应用场景及其实现方式,详细介绍了其在企业级软件开发中的优势和挑战。通过对比事务脚本与领域模型,展示了DDD如何提升系统的可维护性和扩展性。 ... [详细]
  • 本文介绍如何使用布局文件在Android应用中排列多行TextView和Button,使其占据屏幕的特定比例,并提供示例代码以帮助理解和实现。 ... [详细]
  • 在 Flutter 开发过程中,开发者经常会遇到 Widget 构造函数中的可选参数 Key。对于初学者来说,理解 Key 的作用和使用场景可能是一个挑战。本文将详细探讨 Key 的概念及其应用场景,并通过实例帮助你更好地掌握这一重要工具。 ... [详细]
  • 本文详细介绍了如何在Android 4.4及以上版本中配置WebView以实现内容的自动高度调整和屏幕适配,确保中文显示正常,并提供代码示例。 ... [详细]
  • 本文将详细介绍如何在没有显示器的情况下,使用Raspberry Pi Imager为树莓派4B安装操作系统,并进行基本配置,包括设置SSH、WiFi连接以及更新软件源。 ... [详细]
  • 本文介绍了如何通过Unity的UGUI系统创建一个可滚动的页面,包括创建Scroll面板、添加ScrollView面板并配置ScrollRect组件等步骤。 ... [详细]
  • 本文介绍了如何在Java中使用org.apache.commons.math3.linear.ArrayRealVector.getEntry()方法,并提供了多个实际应用中的代码示例。 ... [详细]
  • 1Authenticator简介1.1层次结构图1.2作用职责是验证用户帐号,是ShiroAPI中身份验证核心的入口点;接口中声明的authenticate方法就是用来实现认证逻辑 ... [详细]
  • MainActivityimportandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;im ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有