New Features of Android5.0 (lollipop) (1)

Source: Internet
Author: User

New Features of Android5.0 (lollipop) (1)

At the Google I/O conference in June this year, Android L's first meeting I believe will make many android fans have little excitement and little expectation. Of course, as a developer, I am excited and self-evident, after all, this is the biggest version changed since. The new Design language (Material Design) has over 5000 new APIs. Not to mention nonsense. Today we are talking about a common topic in Android5.0, which is also a record of self-learning.

1. CardView

As the name suggests, the CardView Card view is inherited from framelayout. You can set rounded corners and shadows to show effects like cards.

 

Usage: declare CardView in layout (Note: The android-support-v7-cardview.jar needs to be introduced)

            
 
In the Activity, you can set the rounded corners and shadows of CardView by calling the mCardView. setRadius (progress) and mCardView. setElevation (progress) methods.

 

 

2. RecyclerView

The emergence of RecyclerView can replace ListView and GridView. It standardizes ViewHolder. Previously, we needed to customize ViewHolder when writing the ListView Adapter to increase the ListView speed. LayoutManager must be set when RecyclerView is used. It notifies the System of the layout of the RecyclerView. Currently, the system provides two LayoutManager types, LinearLayoutManager and GridLayoutManager, which correspond to the linearity and grid, of course, you can also customize LayoutManager to meet various needs.

Usage: declare RecyclerView in the layout file (Note: android-support-v7-recyclerview.jar needs to be introduced)

     
          
           
        
    
   
      
 

 

 

Call the following code in the Activity:

 

MRecyclerView = (RecyclerView) rootView. findViewById (R. id. recyclerView); // similar to listviewmRecyclerView. setLayoutManager (new LinearLayoutManager (getActivity (); // similar to grdiviewmRecyclerView. setLayoutManager (new GridLayoutManager (getActivity (), 3); mAdapter = new CustomAdapter (mDataset); // Set CustomAdapter as the adapter for RecyclerView. mRecyclerView. setAdapter (mAdapter );

 

The CustomAdapter code is as follows:

 

public class CustomAdapter extends RecyclerView.Adapter
 
   {    private static final String TAG = CustomAdapter;    private String[] mDataSet;    /**     * Provide a reference to the type of views that you are using (custom ViewHolder)     */    public static class ViewHolder extends RecyclerView.ViewHolder {        private final TextView textView;        public ViewHolder(View v) {            super(v);            // Define click listener for the ViewHolder's View.            v.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                }            });            textView = (TextView) v.findViewById(R.id.textView);        }        public TextView getTextView() {            return textView;        }    }    /**     * Initialize the dataset of the Adapter.     *     * @param dataSet String[] containing the data to populate views to be used by RecyclerView.     */    public CustomAdapter(String[] dataSet) {        mDataSet = dataSet;    }    // Create new views (invoked by the layout manager)    @Override    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {        // Create a new view.        View v = LayoutInflater.from(viewGroup.getContext())                .inflate(R.layout.text_row_item, viewGroup, false);        return new ViewHolder(v);    }    // Replace the contents of a view (invoked by the layout manager)    @Override    public void onBindViewHolder(ViewHolder viewHolder, final int position) {        // Get element from your dataset at this position and replace the contents of the view        // with that element        viewHolder.getTextView().setText(mDataSet[position]);        viewHolder.getTextView().setPadding(0, position, 0, 0);    }    // Return the size of your dataset (invoked by the layout manager)    @Override    public int getItemCount() {        return mDataSet.length;    }}
 

We will introduce FloatActionBar, touch feedback, and live Activity animation in the future.

 

 

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.