Material Design-New widgets (recyclerview cardview)

Source: Internet
Author: User

New widgets:Two new controls are provided.

  • Recyclerview
  • Cardview

These two controls are included in the support library of Android L,

They can be used to display complex la S and all adopt the material style by default.

Recyclerview

Recyclerview is a more advanced listview. He optimized the item view contained in it,

You can use item view to recycle items during scrolling. In fact, this function is in listview

Viewholder.

Recyclerview has the following two features:

  • A layout manager is provided for locating items.
  • The animation effect is added when the item is added or deleted.
    • Recyclerview. itemanimator (this class must be inherited when defining an animation)

    • Recyclerview. setitemanimator (use this method to add the defined animation to recyclerview)

When using recyclerview, you also need to provide the following two variables:

  • Recyclerview. adapter (used to adapt data to the List)
  • Recyclerview. layoutmanager (used to manage item views, such as recycling)
Examples

Create a layout and add a recyclerview.

<!-- 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"/>

Define an 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);        // improve performance if you know that changes in content        // do not change the size of the RecyclerView        mRecyclerView.setHasFixedSize(true);        // 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);    }    ...}

Create a simple Adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {    private String[] mDataset;    // Provide a reference to the type of views that you are using    // (custom viewholder)    public static class ViewHolder extends RecyclerView.ViewHolder {        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, null);        // 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;    }}
Cardview

Cardview inherits from framelayout. This control allows you to display information using CARDS,

All apps can have a unified style. This control can be used to set shadows and rounded corners.

The special attributes of cardview are as follows:

  • Android: cardcornerradius: Set the rounded corner of the card in the layout.
  • Cardview. setradius: Set the rounded corner of the card in the code.
  • Android: cardbackgroundcolor: Set the color of the card background in the layout.
  • Android: elevation: sets the shadow size.

Usage:

 

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

 

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.