热门标签 | 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



推荐阅读
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 在 Flutter 开发过程中,开发者经常会遇到 Widget 构造函数中的可选参数 Key。对于初学者来说,理解 Key 的作用和使用场景可能是一个挑战。本文将详细探讨 Key 的概念及其应用场景,并通过实例帮助你更好地掌握这一重要工具。 ... [详细]
  • 本文介绍了如何在多线程环境中实现异步任务的事务控制,确保任务执行的一致性和可靠性。通过使用计数器和异常标记字段,系统能够准确判断所有异步线程的执行结果,并根据结果决定是否回滚或提交事务。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 实体映射最强工具类:MapStruct真香 ... [详细]
  • 本文详细介绍了 org.jdesktop.swingx.JXTitledPanel 类中的 setUI() 方法,探讨其功能、使用场景,并提供了多个实际代码示例。 ... [详细]
  • 本文介绍如何使用布局文件在Android应用中排列多行TextView和Button,使其占据屏幕的特定比例,并提供示例代码以帮助理解和实现。 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • 在编译BSP包过程中,遇到了一个与 'gets' 函数相关的编译错误。该问题通常发生在较新的编译环境中,由于 'gets' 函数已被弃用并视为安全漏洞。本文将详细介绍如何通过修改源代码和配置文件来解决这一问题。 ... [详细]
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社区 版权所有