Creating a Material Design Android App: Creating Lists and Cards

Creating a Material Design Android App: Creating Lists and Cards

Last time I talked about using themes and applying the Material Design style. At the same time, card layout is also an important part of Material Design. Let’s write about it today.

introduction

To create complex Material Design style List and Card in the program, you can use RecyclerView and CardView components, which are provided in the *** support v7 package (version 21). Therefore, you need to introduce the dependent package:

  1. dependencies {
  2.  
  3. compile 'com.android.support:appcompat-v7:+'  
  4.  
  5. compile 'com.android.support:cardview-v7:+'  
  6.  
  7. compile 'com.android.support:recyclerview-v7:+'  
  8.  
  9. }

Creating a List

The RecyclerView component is a more efficient and flexible ListView. This component is a container for displaying large data sets, which can scroll efficiently and keep a certain number of views displayed. Use the RecyclerView component when you have a data set and the elements of the data set change at runtime based on user operations or network events.

The RecylerView class simplifies the display and processing of large data sets by providing:

Layout managers control the positioning of elements.

Displays default animations for common element operations, such as removing and adding elements.

To use the RecyclerView component, you need to specify an Adapter and a layout manager. Create an Adapter that extends the RecyclerView.Adapter class. The specific implementation details vary depending on the type of data collection view. For more information, see the following example.

A layout manager positions the Item view in the RecyclerView and decides when to recycle it when it is no longer visible. When reusing (or recycling) a view, the layout manager may request the adapter to replace the content in the subview with different content. Recycling and reusing views in this way can reduce the creation of views and avoid more findViewById(), thereby improving performance.

RecyclerView provides the following built-in layout managers:

LinearLayoutManager displays Item in a horizontal or vertical scrolling list.

GridLayoutManager displays Item as a grid layout.
StaggeredGridLayoutManager displays Item in a staggered grid layout.

You can also create your own custom layout manager by inheriting the RecyclerView.LayoutManager class.

RecylerView Component

Animation:

RecyclerView has animation by default when deleting or adding Items. If you need a custom animation, inherit the RecyclerView.ItemAnimator class and use the RecyclerView.setItemAnimator() method to set the defined animation to our view.

Let's start with an example:

1. First add a RecyclerView to the xml layout file

  1. <!-- A RecyclerView with some commonly used attributes -->  
  2.  
  3. < android.support.v7.widget.RecyclerView  
  4.  
  5. android:id = "@+id/my_recycler_view"  
  6.  
  7. android:scrollbars = "vertical"  
  8.  
  9. android:layout_width = "match_parent"  
  10.  
  11. android:layout_height = "match_parent" />

2. Then use it in our Java code, attach the Adapter and data and it will be displayed.

  1. public   class MyActivity extends Activity {
  2.  
  3. private RecyclerView mRecyclerView;
  4.  
  5. private RecyclerView.Adapter mAdapter;
  6.  
  7. private RecyclerView.LayoutManager mLayoutManager;
  8.  
  9. @Override  
  10.  
  11. protected   void onCreate(Bundle savedInstanceState) {
  12.  
  13. super .onCreate(savedInstanceState);
  14.  
  15. setContentView(R.layout.my_activity);
  16.  
  17. mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
  18.  
  19. // use this setting to improve performance if you know that changes  
  20.  
  21. // in content do not change the layout size of the RecyclerView  
  22.  
  23. mRecyclerView.setHasFixedSize( true );
  24.  
  25. // use a linear layout manager  
  26.  
  27. mLayoutManager = new LinearLayoutManager( this );
  28.  
  29. mRecyclerView.setLayoutManager(mLayoutManager);
  30.  
  31. // specify an adapter (see also next example)  
  32.  
  33. mAdapter = new MyAdapter(myDataset);
  34.  
  35. mRecyclerView.setAdapter(mAdapter);
  36.  
  37. }
  38. ...
  39. }

3. The Adapter provides access to the Items in the data set, creates a view mapped to the data, and replaces the content data of the layout with the new item. The following code shows a simple implementation, using a TextView to display a simple String array.

  1. public   class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  2.  
  3. private String[] mDataset;
  4.  
  5. // Provide a reference to the views for each data item  
  6.  
  7. // Complex data items may need more than one view per item, and  
  8.  
  9. // you provide access to all the views for a data item in a view holder  
  10.  
  11. public   static   class ViewHolder extends RecyclerView.ViewHolder {
  12.  
  13. // each data item is just a string in this case  
  14.  
  15. public TextView mTextView;
  16.  
  17. public ViewHolder(TextView v) {
  18.  
  19. super (v);
  20.  
  21. mTextView = v;
  22.  
  23. }
  24.  
  25. }
  26.  
  27. // Provide a suitable constructor (depends on the kind of dataset)  
  28.  
  29. public MyAdapter(String[] myDataset) {
  30.  
  31. mDataset = myDataset;
  32.  
  33. }
  34.  
  35. // Create new views (invoked by the layout manager)  
  36.  
  37. @Override  
  38.  
  39. public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
  40.  
  41. int viewType) {
  42.  
  43. // create a new view  
  44.  
  45. View v = LayoutInflater.from(parent.getContext())
  46.  
  47. .inflate(R.layout.my_text_view, parent, false );
  48.  
  49. // set the view's size, margins, paddings and layout parameters  
  50.  
  51. ...
  52.  
  53. ViewHolder vh = new ViewHolder(v);
  54.  
  55. return vh;
  56.  
  57. }
  58.  
  59. // Replace the contents of a view (invoked by the layout manager)  
  60.  
  61. @Override  
  62.  
  63. public   void onBindViewHolder(ViewHolder holder, int position) {
  64.  
  65. // - get element from your dataset at this position  
  66.  
  67. // - replace the contents of the view with that element  
  68.  
  69. holder.mTextView.setText(mDataset[position]);
  70.  
  71. }
  72.  
  73. // Return the size of your dataset (invoked by the layout manager)  
  74.  
  75. @Override  
  76.  
  77. public   int getItemCount() {
  78.  
  79. return mDataset.length;
  80.  
  81. }
  82. }

Create Card

CardView inherits the FrameLayout class, which allows you to display information inside the card and have a unified style on different platforms. The CardView component can have shadows and rounded corners.

To create a card with a shadow, use the card_view:cardElevation property. CardView uses real height and dynamic shadows on Android 5.0 (API 21) and higher, and earlier versions use traditional shadows.

Use these properties to customize the appearance of CardView:

Use the card_view:cardCornerRadius attribute to set the radius of the rounded corners in the layout file.

Use the CardView.setRadius method to set the radius of the rounded corners in the Java code.
To set the background color of a card, use the card_view:cardBackgroundColor attribute.

Here is an example of including a CardView in an XML layout file:

  1. < LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.  
  3. xmlns:tools = "http://schemas.android.com/tools"  
  4.  
  5. xmlns:card_view = "http://schemas.android.com/apk/res-auto"  
  6.  
  7. ... >  
  8.  
  9. <!-- A CardView that contains a TextView -->  
  10.  
  11. < android.support.v7.widget.CardView  
  12.  
  13. xmlns:card_view = "http://schemas.android.com/apk/res-auto"  
  14.  
  15. android:id = "@+id/card_view"  
  16.  
  17. android:layout_gravity = "center"  
  18.  
  19. android:layout_width = "200dp"  
  20.  
  21. android:layout_height = "200dp"  
  22.  
  23. card_view:cardCornerRadius = "4dp" >  
  24.  
  25. < TextView  
  26.  
  27. android:id = "@+id/info_text"  
  28.  
  29. android:layout_width = "match_parent"  
  30.  
  31. android:layout_height = "match_parent"   />  
  32.  
  33. </ android.support.v7.widget.CardView >  
  34.  
  35. </LinearLayout>  

Card example image

Random

From the above, you can see RecyclerView. It is very similar to the ListView we often use, but its parent class is not AbsListView, so it cannot be used together. However, it can replace ListView in many places. Through ViewHolder and View reuse, you can see that this is a more efficient view component and is recommended.

CardView is essentially a component that is more in line with Material Design. It uses Card layout for better results. Many people may have used CardUi before. Google officially released this and it is highly recommended.

The RecyclerView and CardView above are written separately, but we can use them together, don't be confused.

Reference: http://developer.android.com/training/material/lists-cards.html

Original address: http://blog.isming.me/2014/10/21/creating-app-with-material-design-two-list/, please indicate the source when reprinting.

<<:  Android common tools source code collection

>>:  WeChat Enterprise Accounts Dialogue with Enterprise Mobile Platform

Recommend

Thales: The first person in the childhood of science丨Expand the scroll

Thales, an ancient Greek mathematician and philos...

Is it complicated to develop an avatar creation app?

Whether it is a WeChat avatar, a QQ avatar, or an...

6 tips to improve push opening rate of APP!

The main purpose of push is to promote activation...

How much do you know about performance optimization?

[[196882]] 1. Introduction Recently, a new versio...

Ye Tan Finance "2021 Tan Tan Bull and Bear Exchange"

Ye Tan Finance "2021 Tan Tan Bull and Bear E...

7 minefields of big data marketing, how many have you stepped on?

Nowadays, when talking about marketing, if you do...

Can marketing really do more with less money?

There are some things that you cannot think about...

Marine microbes that "eat" debris: What happens when fungi meet plastic?

Produced by | Science Popularization China Author...

Seeing this WeChat update, I decided to praise the founder

[[411358]] I believe that friends who have checke...

If you don't pick up "trash" in autumn, you've wasted your whole year on earth

In autumn, we see a lot of fallen leaves, fruits,...

Prepare for Double Eleven, direct e-commerce holiday marketing plan!

This article shares all aspects of direct-operate...

How does APP choose marketing and promotion channels?

Q: How to choose marketing promotion channels ? A...

How to conduct data analysis for operational promotion?

Talking about data analysis theory alone is too d...