Create a Material Design-style Android app: Create a list and a card

Source: Internet
Author: User

Create a Material Design-style Android app: Create a list and a card

The last time I talked about using theme, applying the Material Design style, and card layout is also an important part of Material Design. I will write it today.

Introduction

Create a complex List and Card of the Material Design style in the program. You can use the RecyclerView and CardView components, which are provided in the latest support v7 package (version 21. Therefore, we need to introduce the dependency 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. }

Create List

The RecylerView component is a more efficient and flexible ListView. When this component is used, a container that displays large datasets can effectively scroll and maintain a certain number of views. When you use the RecyclerView component, when you have a dataset and its elements change according to your operations or network events during running.

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

Layout Manager Control Element positioning.

Display default animations on common elements, such as removing and adding elements.

To use the RecyclerView component, you must specify an Adapter and layout manager and create an Adapter to inherit the RecyclerView. adapter class, the specific implementation details should vary according to the type of the data set view. For more information, see the following example.

A layout manager locates the Item view in the RecyclerView and determines when to recycle it when it is no longer visible. When reusing or recycling a view, the layout manager can request the Adapter) to replace the content in the subview with different content. In this way, reuse views can be recycled to reduce the creation of views and avoid more findViewById (), thus improving performance.

RecyclerView provides the following built-in layout manager:

LinearLayoutManager displays items in a horizontal or vertical scroll list.

GridLayoutManager displays items as grid la S.
StaggeredGridLayoutManager displays the layout of items on the staggered grid.

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

RecylerView component

Animation:

RecyclerView has animations by default. When you delete or add an Ite. To customize an animation, inherit the RecyclerView. ItemAnimator class and use the RecyclerView. setItemAnimator () method to set the animation to our view.

The following is an example:

1. First add a RecyclerView in 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. Use it in our Java code. append the Adapter and data to display it.

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

3. the Adapter provides access to items in the dataset, creates a view mapped to the data, and replaces the layout content data with the new Item. The following code shows a simple implementation. TextView is used 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. // Provide a suitable constructor (depends on the kind of dataset) 
  26.  
  27. public MyAdapter(String[] myDataset) { 
  28.  
  29. mDataset = myDataset; 
  30.  
  31.  
  32. // Create new views (invoked by the layout manager) 
  33.  
  34. @Override 
  35.  
  36. public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
  37.  
  38. int viewType) { 
  39.  
  40. // create a new view 
  41.  
  42. View v = LayoutInflater.from(parent.getContext()) 
  43.  
  44. .inflate(R.layout.my_text_view, parent, false); 
  45.  
  46. // set the view's size, margins, paddings and layout parameters 
  47.  
  48. ... 
  49.  
  50. ViewHolder vh = new ViewHolder(v); 
  51.  
  52. return vh; 
  53.  
  54.  
  55. // Replace the contents of a view (invoked by the layout manager) 
  56.  
  57. @Override 
  58.  
  59. public void onBindViewHolder(ViewHolder holder, int position) { 
  60.  
  61. // - get element from your dataset at this position 
  62.  
  63. // - replace the contents of the view with that element 
  64.  
  65. holder.mTextView.setText(mDataset[position]); 
  66.  
  67.  
  68. // Return the size of your dataset (invoked by the layout manager) 
  69.  
  70. @Override 
  71.  
  72. public int getItemCount() { 
  73.  
  74. return mDataset.length; 
  75.  

Create a Card

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

Create a shadow Card and use the card_view: cardElevation attribute. CardView uses real height and dynamic shadows in Android5.0 (API 21) and later versions, while earlier versions use traditional shadows.

Use these attributes to customize the appearance of CardView:

Use the card_view: cardCornerRadius attribute to set the radius of the rounded corner.

Use the CardView. setRadius method to set the radius of the rounded corner in java code.
Set the background color of the card and use the card_view: cardBackgroundColor attribute.

The following is an example of 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

Chaos

You can see the RecyclerView above. It is similar to the ListView we often use, but its parent class is not AbsListView, so it cannot be mixed for use. However, you can replace ListView in many places. By reusing ViewHolder and View, you can see that this is a more efficient View component, which is recommended.

CardView is essentially a component that conforms to the Material Design and uses the Card layout for better results. Many people may have used some CardUi before. Google officially published this and it is strongly recommended to use it.

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

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

Original article address: Workshop.

Related Article

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.