The use of Recyclerview helloworld_android

Source: Internet
Author: User
Tags static class

Recyclerview has been on the market for a long time and has been widely used in many applications and has a good reputation throughout the developer community, which means that Recyclerview has many advantages over controls such as: Data binding, Item View creation, view recycling, and reuse mechanisms.

Recyclerview is a new control that accompanies the Android 5.0 release and is a list container that Google intends to use to replace the old ListView and GridView with New Recyclerview, which is more flexible and performance than ListView, Next through a series of articles to understand the various uses of Recyclerview, this article to introduce its initial use, Recyclerview "Helloword."

The difference between Recyclerview and traditional ListView:

1:viewholder mode, traditional ListView can improve the performance of list scrolling by Viewholder, but this is not necessary because ListView has no strict standard design pattern. However, when using Recyclerview, adapter must implement at least one viewholder, because it has a strict viewholder design pattern.

2: Display effect, ListView can only achieve vertical scrolling list view, instead, Recyclerview can customize different styles of view by setting Recyclerview.layoutmanager, such as horizontal scrolling list or irregular waterfall flow list.

3: The list item animation, does not provide any method or the interface in the ListView, facilitates the developer to realize the item additions and deletions animation. Recyclerview can add animation effects to an entry by setting the Recyclerview.itemanimator.

This article to realize the following small demo to understand the initial use of Recyclerview

* Photo material copyright belongs to

1: Dependent libraries (this article takes Android Studio as a development tool)

Gradle Configuration
Compile ' '

2: Establish layout, similar to ListView, add Recyclerview in Layout

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent ">
xmlns:android= "Http://"
Android:layout_width= "Match_parent"
android:layout_height= "match_parent"
android:id= "@+id/rv_list"

3: Establish Recyclerview Item layout

<?xml version= "1.0" encoding= "Utf-8"?> < xmlns:card_view= "http://" "xmlns:android=" Http:// "Android:layout_
Width= "Match_parent" android:layout_height= "wrap_content" android:layout_margin= "8DP" android:id= "@+id/cv_item" Android:foreground= "Android:attr/selectableitembackground" card_view:cardcornerradius= "4DP" Card_view: Cardbackgroundcolor= "#795548" card_view:cardelevation= "4DP" > <linearlayout android:layout_width= "Match_ Parent "android:layout_height=" wrap_content "android:orientation=" vertical "> <imageview android:id=" @+id/iv_
Pic "android:layout_width=" match_parent "android:layout_height=" 200DP "android:layout_weight=" 1 "/> <TextView
Android:id= "@+id/tv_text" android:padding= "20DP" android:textcolor= "#ffffff" android:layout_width= "Match_parent" android:layout_height= "Wrap_content"/> </LinearLayout> </>

Here, a control called CardView, inherited from Framelayout, is a card-view container provided by Google that makes it easy to display a card-style layout with shadows and rounded corners, like this

CardView needs to be imported before it can be used like Recyclerview.

Compile ' '

4: Then establish the Recyclerview adapter

Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ImageView;
Import Android.widget.TextView;
Import Android.widget.Toast;
/** * Created by Lijizhou on 2016/2/3.
* * public class Recyclerviewadapter extends Recyclerview.adapter<recyclerviewadapter.normalviewholder> {
Private Layoutinflater Mlayoutinflater;
Private context Mcontext;
Private String [] mtitle;
private int [] mpic;
Public Recyclerviewadapter (Context context,string[]title,int[] pic) {mcontext=context; mtitle=title; mpic=pic;
Mlayoutinflater=layoutinflater.from (context); }//Custom Viewholder, holding all interface elements for each item public static class Normalviewholder extends recyclerview.viewholder{TextView
CardView Mcardview;
ImageView Mimageview; Public Normalviewholder (View Itemview) {super (Itemview); mtextview= (TextView) Itemview.findviewbyid (R.Id.tv_text);
mcardview= (CardView) Itemview.findviewbyid (;
mimageview= (ImageView) Itemview.findviewbyid (; In this method we create a viewholder and return, Viewholder must have a constructor with a view, which is the root layout of our item, where we use the layout of the custom item; @Override Public Normalviewholder Oncreateviewholder (viewgroup parent, int viewtype) {return new Normalviewholder (
Mlayoutinflater.inflate (R.layout.item_view,parent,false)); The operation of binding data to the interface @Override public void Onbindviewholder (Normalviewholder holder, final int position) {Holder.mtextview.
SetText (Mtitle[position]);
Holder.mImageView.setBackgroundResource (Mpic[position]); Holder.mCardView.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {Toast.maketext
(mcontext,mtitle[position],3000). Show ();
}); ///Get the number of data @Override public int getitemcount () {return mtitle==null? 0:mtitle.length}}

public class Recycleviewactivity extends Appcompatactivity {private Recyclerview Mrecyclerview;//item Display required Private stri Ng[] title = {Blog:, "A good laugh and a long sleep are the best cures in the doctor ' s Book. "," All or no, now or never "," is nice to people on the way up, because you'll need them on your way down. "," B E confident with yourself and stop worrying what the other people.  Do what's best for your future happiness! "," Blessed are he whose fame does does outshine his truth. "," Create good memories
Today, so this you can have a good past "};  /** * Photo Resources Copyright belongs to * * Private int[] pic = {R.MIPMAP.AA1, r.mipmap.aa0, R.mipmap.aa2, R.mipmap.aa3, R.MIPMAP.AA4,
R.mipmap.aa5, R.MIPMAP.AA6}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
Mrecyclerview = (Recyclerview) Findviewbyid (; Create a linear layout manager Linearlayoutmanager LayoutmanaGER = new Linearlayoutmanager (this);
Set vertical scrolling, or you can set horizontal scrolling layoutmanager.setorientation (linearlayoutmanager.vertical); The other two display modes//Mrecyclerview.setlayoutmanager (new Gridlayoutmanager (this, 2)); Grid View//Mrecyclerview.setlayoutmanager (New Staggeredgridlayoutmanager (2, orientationhelper.vertical));
Here the Linear Sudoku display resembles the waterfall flow//recyclerview Settings Layout Manager mrecyclerview.setlayoutmanager (LayoutManager);
Recyclerview set Up Adapter Mrecyclerview.setadapter (this, title, pic) (new recyclerviewadapter); }

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