Analysis of the creation method of list and card in Android material design _android

Source: Internet
Author: User
Tags static class recyclerview android

5.0 offers two new widgets that use the style and animation of the material design:

    • Recyclerview a more pluggable ListView that supports different layout types and has improved performance. (list type)
    • CardView A card that allows you to display important information inside and maintain a coherent vision and feeling. (Card type)

It's two in Sdk/extras/android/support/v7/cardview and Sdk/extras/android/support/v7/recyclerview.


Create a list
The Recyclerview component is a more advanced and flexible version of the list view. This widget is a very efficient container, and with limited views, you can scroll through the display of large datasets.
Recyclerview elements of a component data collection that can be changed at run time based on user actions or network events.
The Recyclerview class simplifies the display and processing of large datasets, which provides:

    • Layout Manager
    • Common default animation item actions, such as deleting, adding items

You can flexibly define layout manager and animation 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 following example.
Recyclerview and determines that when reusing a project view, the layout manager's method of using item is no longer visible to the user. Reuse (or Reclaim) the view, the layout manager may ask the adapter to replace elements of the different dataset. When you recycle view, you improve performance in this way: avoid creating unnecessary view or performing a Findviewbyid () query that consumes a large size.

Recyclerview provides the following manager:

    • Linearlayoutmanager a horizontal or vertical scrolling list
    • Gridlayoutmanager Grid List
    • Staggeredgridlayoutmanager Staggered Grid list

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

Animation:
Adds and deletes an item's animation, which is enabled by default in Recyclerview. To customize these animations, you need to inherit the Recyclerview.itemanimator class and use 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 your know that changes//in content did not change the layout size of th E Recyclerview mrecyclerview.sethasfixedsize (true); 
    Use fixed size to optimize performance/Linear layout manager Mlayoutmanager = new Linearlayoutmanager (this); 
 
    Mrecyclerview.setlayoutmanager (Mlayoutmanager); 
    Specify an adapter (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/ 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 of the public TextView Mtextview; 
      Public Viewholder (TextView v) {super (V); 
    Mtextview = v; 
    }//provide a suitable constructor (depends on the kind of datasets) 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 TextView v = (TextView) Layouti 
    Nflater.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 (view Holder 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 to the size of your dataset (invoked by the layout manager) @Override public int GetItemCount () { 
  return mdataset.length; 

 } 
}

Create a card
CardView inherits from Framelayout, displaying a consistent appearance in a card style. It can have shadows and rounded corners.
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 fillet radii in code using the Cardview.setradius method
    • Set the background color of the card, using the Card_view:cardbackgroundcolor property

Cases

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

Add Dependencies:
Gradle dependence

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

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.