Creating Apps With Material Design —— Creating Lists and Cards

简介: 转载请注明 http://blog.csdn.net/eclipsexys 翻译自Developer Android,时间仓促,有翻译问题请留言指出,谢谢创建Lisst和Cards在你的应用程序创建复杂的清单,并与材料设计风格卡,您可以使用RecyclerView和CardView部件。

转载请注明 http://blog.csdn.net/eclipsexys 翻译自Developer Android,时间仓促,有翻译问题请留言指出,谢谢


创建Lisst和Cards


在你的应用程序创建复杂的清单,并与材料设计风格卡,您可以使用RecyclerView和CardView部件。 


创建RecyclerView 


该RecyclerView widget是ListView中的更先进,更灵活的版本。这个小工具是一个容器,用于显示,能非常有效地维护了意见数量有限,滚动大的数据集。当你有收集数据,它的元素在运行时改变基于用户行为和网络事件使用RecyclerView部件。 


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

    定位项目布局管理器 
    默认的动画为公用项的操作,例如删除或增加的项目 

您还可以在RecyclerView部件定义自定义布局管理器和动画的灵活性。


要使用RecyclerView小部件,你必须指定一个适配器和一个布局管理器。要创建一个适配器,扩展RecyclerView.Adapter类。实施的细节取决于你的数据集的具体情况和意见的类型。欲了解更多信息,请参见下面的例子。 

RecyclerView内部的布局管理器的位置的项目的意见,并确定何时重用项目的看法不再对用户可见。重用(或回收)的图,布局管理器可能会问适配器与数据集不同的元素替换视图的内容。以这种方式回收的观点提高通过避免产生不必要的视图或执行昂贵findViewById()的查找性能。 

RecyclerView提供这些内置的布局管理器: 

    LinearLayoutManager显示在垂直或水平滚动列表项。 
    GridLayoutManager显示在网格中的项目。 
    StaggeredGridLayoutManager显示了交错网格项目。 

要创建自定义布局管理器,扩展RecyclerView.LayoutManager类。 


动画 


动画的添加和删除项目中默认RecyclerView启用。要自定义这些动画,继承RecyclerView.ItemAnimator类,并使用RecyclerView.setItemAnimator()方法。




Examples


下面的代码示例演示如何将RecyclerView添加到布局:

<!-- 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"/>

一旦你添加了一个RecyclerView小部件布局,获取句柄的对象,将其连接到一个布局管理器,并附上要显示的数据适配器:

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);

        // 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);
    }
    ...
}

该适配器提供访问信息在您的数据集,创建视图的项目,取代了一些新的数据项的视图的内容时,原来的产品不再可见。下面的代码示例显示了一个简单的实现,它由一个字符串数组的使用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
    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 = 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

CardView扩展的FrameLayout类,并允许你显示里面有跨平台一致的外观卡的信息。 CardView部件可以有阴影和圆角。 

要创建具有阴影卡,使用card_view:cardElevation属性。 CardView使用真实高程和动态阴影在Android5.0(API等级21)以上,并回落到较早版本的纲领性阴影实施。欲了解更多信息,请参见维护兼容性。 

使用这些属性来定制CardView小部件的外观: 

    要设置圆角半径在你的布局,使用card_view:cardCornerRadius属性。 
    要设置圆角半径在你的代码中,使用CardView.setRadius方法。 
    设置卡的背景颜色,使用card_view:cardBackgroundColor属性。 

下面的代码示例显示了如何在您的布局CardView部件:

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


目录
相关文章
|
9月前
|
设计模式 缓存 监控
译|Design patterns for container-based distributed systems(下)
译|Design patterns for container-based distributed systems(下)
39 0
|
9月前
|
设计模式 分布式计算 Kubernetes
译|Design patterns for container-based distributed systems(上)
译|Design patterns for container-based distributed systems
45 0
SAP RETAIL Allocation Rule based on Material Group(二)
SAP RETAIL Allocation Rule based on Material Group(二)
SAP RETAIL Allocation Rule based on Material Group(二)
SAP RETAIL Allocation Rule based on Material Group(一)
SAP RETAIL Allocation Rule based on Material Group(一)
SAP RETAIL Allocation Rule based on Material Group(一)
|
Web App开发
How to trouble shoot if there is no entityset available when creating a tile
How to trouble shoot if there is no entityset available when creating a tile
How to trouble shoot if there is no entityset available when creating a tile
sap.ca.ui.utils.busydialog - scenario1 - opportunity opened
Created by Wang, Jerry, last modified on Jun 29, 2015
90 0
sap.ca.ui.utils.busydialog - scenario1 - opportunity opened
|
XML 数据格式
Some more technical details about SAP note
I use this note 2184333 which I am responsible for as an example:
121 0
Some more technical details about SAP note
strange behavior:why u31000 is accessed for Extension project
Created by Wang, Jerry, last modified on May 20, 2015
92 0
strange behavior:why u31000 is accessed for Extension project