Android (lollipop/5.0) Material Design (iv) Create lists and cards

Source: Internet
Author: User

Material Design Series

Android (lollipop/5.0) Material Design (a) Brief introduction

Android (lollipop/5.0) Material Design (ii) Getting Started Guide

Android (lollipop/5.0) Material Design (iii) using Material themes

Android (lollipop/5.0) Material Design (iv) Create lists and cards

Android (lollipop/5.0) Material Design (v) define shadows and crop view

Android (lollipop/5.0) Material Design (vi) using pictures

Android (lollipop/5.0) Material Design (vii) Self-defining animations

Android (lollipop/5.0) Material Design (eight) Maintain compatibility


Official website: https://developer.android.com/training/material/lists-cards.html

In your application, create complex lists and cards with material design styles. You can use Recyclerview and CardView parts.


The Create List Recyclerview component is a more advanced and flexible version number for a list view.

This widget is a very efficient container, with limited views. Ability to scroll to display large datasets. An element of the Recyclerview component data collection that can be changed at execution time based on user actions or network events.


The Recyclerview class simplifies displaying and processing large datasets, providing:

· Layout Manager

· Common default animated item operations, such as deleting, joining projects

You can flexibly define layout managers and animations in Recyclerview



To use the Recyclerview component, you must specify an adapter and layout manager. Create an adapter that inherits the Recyclerview.adapter class. For a lot of other information, see the sample below.

Recyclerview and determines how the layout manager takes advantage of item when reusing project views. is no longer visible to the user. reusing (or reclaiming) a view, the layout manager may ask the adapter to replace the element with a different data set.

When you recycle a view, you improve performance in such a way that you avoid creating unnecessary view or running a Findviewbyid () query that is expensive.



Recyclerview provides for example the following manager:

· Linearlayoutmanager scrolling list in landscape or portrait

· Gridlayoutmanager Grid List

· Staggeredgridlayoutmanager Staggered Grid list

To create a self-defined layout manager, you need to inherit the Recyclerview.layoutmanager class


Animations to add and remove item animations. Enabled by default on Recyclerview. Customizing these animations requires inheriting the Recyclerview.itemanimator class and using the Recyclerview.setitemanimator () method.



Sample 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 so changes//in content does not change the        layout size of the Recyclerview        Mrecyclerview.sethasfixedsize (TRUE);//using a fixed size to optimize performance        //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 "view for each data item//COMPLEX data items * need more than one views per item, and You provide access to all of the views for a data item in a view holder public static class Viewholder extends Recyc        Lerview.viewholder {//each of the data item is just a string "in the" case "public TextView Mtextview;            Public Viewholder (TextView v) {super (V);        Mtextview = v;        }}//provide a suitable constructor (depends on the kind in dataset) public Myadapter (string[] mydataset) {    Mdataset = myDataSet; }//Create new Views (invoked by the layout manager) @Override public Myadapter.viewholder Oncreateviewholder (Vi Ewgroup parent, int viewtype) {//Create a new view TEXTV Iew 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 (viewho Lder holder, int position) {//-get element from your dataset at this position//-Replace the contents O    f 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; }}

Create cards CardView inherit from Framelayout, showing a consistent appearance in a card style. It can have shadows and fillets to create a shaded card, using the Card_view:cardelevation property.
Use these properties to customize the appearance of the CardView component:
· Set the fillet radius in your layout, using the Card_view:cardcornerradius property · Set the fillet radius in the code, using the Cardview.setradius method · Set the background color of the card, using the Card_view:cardbackgroundcolor attribute example
<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 to    <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>

Join Dependent Gradle Dependency
dependencies {    ...    Compile ' com.android.support:cardview-v7:21.0.+ '    compile ' com.android.support:recyclerview-v7:21.0.+ '}



Android (lollipop/5.0) Material Design (iv) Create lists and cards

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.