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

Source: Internet
Author: User

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

previous section : using the material theme

next section : Defining Shadows and cropping view


In your application, create complex lists and card and material design styles, you can use Recyclerview and CardView widgets.


The Create List Recyclerview component is a more advanced and flexible version of the list view. This widget is a very efficient container that lets you scroll through a large data set with a limited view. An element of the Recyclerview component data collection that can be changed at run time based on user actions or network events.

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

· Layout Manager

· Common default animation item operations, such as deleting, adding items

You can flexibly define the layout manager 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 more information, see the example below.

Recyclerview and determines when reusing project views, the layout manager's method of leveraging item 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 this way: avoid creating unnecessary view or executing expensive Findviewbyid () queries.


Recyclerview provides the following manager:

· Linearlayoutmanager scrolling list in landscape or portrait

· Gridlayoutmanager Grid List

· Staggeredgridlayoutmanager Staggered Grid list

To create a custom layout manager, you need to inherit the Recyclerview.layoutmanager class


Animations add and remove item animations, which are enabled by default in Recyclerview. Customizing these animations requires inheriting the Recyclerview.itemanimator class and using the Recyclerview.setitemanimator () method.

Example 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 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 (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 that uses 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>

Adding dependent gradle dependencies
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.