Android (Lollipop/5.0) Material Design (4) create a list and a card

Source: Internet
Author: User

Android (Lollipop/5.0) Material Design (4) create a list and a card

 

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

 

The create list RecyclerView component is a more advanced and flexible version list view. This widget is a very efficient container. With Limited views, you can scroll to display large datasets. Elements of the RecyclerView component data set, which can be changed at runtime based on user operations or network events.

The RecyclerView class simplifies the display and processing of large datasets. It provides:

· Layout manager

· Common default animation item operations, such as deleting and adding projects

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

When RecyclerView and re-use the project view, the layout manager uses the item method and is no longer visible to users. When you reuse (or recycle) views, the layout manager may ask the adapter to replace the content with elements of different datasets. This method is used to improve the performance when the view is recycled: Avoid creating unnecessary views or execute findViewById () queries that consume a large amount.

 

RecyclerView provides the following Manager:

· LinearLayoutManager horizontal or vertical scrolling list

· GridLayoutManager grid list

· StaggeredGridLayoutManager staggered grid list

To create a custom layout manager, you must inherit the RecyclerView. LayoutManager class.

 

The animation for adding or deleting items is enabled by default in RecyclerView. To customize these animations, you must inherit the RecyclerView. ItemAnimator class and use the RecyclerView. setItemAnimator () method.

Example: layout
 

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 that changes // in content do not change the layout size of the RecyclerView mRecyclerView. setHasFixedSize (true); // use 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
 
   {    private String[] mDataset;    // Provide a reference to the views for each data item    // Complex data items may need more than one view per item, and    // you provide access to all the views for a data item in a view holder    public static class ViewHolder extends RecyclerView.ViewHolder {        // each data item is just a string in this case        public TextView mTextView;        public ViewHolder(TextView v) {            super(v);            mTextView = v;        }    }    // Provide a suitable constructor (depends on the kind of dataset)    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        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(ViewHolder 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 the size of your dataset (invoked by the layout manager)    @Override    public int getItemCount() {        return mDataset.length;    }}
 

The created card CardView inherits from FrameLayout and displays consistent appearances on a card. It can create a shadow card with shadow and rounded corners and use the card_view: cardElevation attribute.
Use these attributes to customize the appearance of the CardView component:
· Set the Corner radius in your layout and use the card_view: cardCornerRadius attribute · set the Corner radius in the Code and use the CardView. setRadius method · set the background color of the card and use the card_view: cardBackgroundColor attribute example.
     
              
      
  
 

Add gradle dependency
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.