Introducedon Google's website we can see how it is presented:
RecyclerView
is a more advanced and flexible version of
ListView
. This widget was a container for large sets of views of the can be recycled and scrolled very efficiently. Use the
RecyclerView
The widget is lists with elements the change dynamically.
Recyclerview is more advanced and flexible than the ListView, and for many views it is a container that can be reused and scrolled efficiently. Use it when the data changes dynamically.
RecyclerView
is because it provides:
- A layout Manager for positioning items
- Default animations for common item operations
- You also has the flexibility to define custom layout managers and animations for this widget.
Recyclerview is convenient to use because it provides:
It provides a layoutmanager for the positioning of the item
Provides a default animations for the operation of the item
You also have the flexibility to define custom layout managers and animations for this widget
to use theRecyclerView
widget, you has to specify an adapter and a layout manager. To create a adapter, you extend theRecyclerView.Adapter
class. The details of the implementation depend on the specifics of your datasets and the type of views. For more information, see theExamplesbelow.
In order to use Recyclerview, you must specify a Adapter and a layoutmanager, in order to create a Adapter, you have to inherit Recyclerview.adapter, The detailed implementation method depends on your dataset and the type of your view.
Demo presentation differs from official website
Here is the introduction of the following we are going to do their own demo. If you need a demo of the crossing network, please open it here: Official demo
Since this is a detailed explanation then there will be different from the official demo, so look at the effect we have to do.
Implements the blending of picture text buttons.
First of all, look at my engineering structure.
First of all, I'm sticking out my main_acitivy.xml.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "Vertical" > <!--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"/></linearlayout>
A few other XML does not have to post, very simple put write textview,imgeview and so on.
Then we'll take a look at the code, first of all the code inside the bean.
Package com.androidl.bob;/** * Entity Pack * * @author Edsheng * */public class Bean {public static final int y_type = 0; View type 0public static final int x_type = 1; View type 2public static final int z_type = 2;//view type 3private int type;private string text;public Bean (int type, string te XT) {super (); this.type = Type;this.text = text;} public int GetType () {return type;} public void SetType (int type) {this.type = type;} Public String GetText () {return text;} public void SetText (String text) {this.text = text;}}
Then there is the code inside the adapter:
Package Com.androidl.bob;import Java.util.list;import Com.example.androidl.r;import Android.support.v7.widget.recyclerview;import Android.support.v7.widget.recyclerview.viewholder;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.Button; Import Android.widget.imagebutton;import Android.widget.imageview;import Android.widget.textview;import android.widget.toast;/** * DATE:2014/7/15 * * @author Edsheng * */public class Recycleadapter extends Recyclerview.ada pter<viewholder> {private list<bean> beans;public recycleadapter (list<bean> beans) {super (); This.beans = beans;} /** * Internal Textholer * * @author Edsheng * */public class Textholer extends Recyclerview.viewholder {public TextView TEXTVI Ew;public Textholer (View textview) {super (TextView); This.textview = (TextView) Textview.findviewbyid (R.id.mytext);}} /** * Iamgeholder * * @author Edsheng * */public class Imageholer extends Recyclerview.viewholder {public ImageView imageview;public Imageholer (View textview) {super (TextView); Imageview = (Imageview) Textview.findviewbyid (R.ID.MYIAMGE);}} /** * Button Holder * * @author Edsheng * */public class Buttonholder extends Recyclerview.viewholder {public button button; Public Buttonholder (View TextView) {super (TextView); This.button = (Button) Textview.findviewbyid (R.id.mybutton);}} @Overridepublic int GetItemCount () {//TODO auto-generated method Stubreturn beans.size ();} /** * Gets the type of message */@Overridepublic int getitemviewtype (int position) {//TODO auto-generated method Stubreturn Beans.get (posi tion). GetType ();} /** * Create Viewholder */@Overridepublic Viewholder Oncreateviewholder (viewgroup parent, int viewtype) {//TODO auto-generate D method Stubview v = null; Viewholder Holer = Null;switch (viewtype) {Case bean.x_type:v = Layoutinflater.from (Parent.getcontext ()). Inflate ( r.layout.recylce_item_x, null); Holer = new Textholer (v); Break;case bean.y_type:v = Layoutinflater.from ( Parent.getcontext ()). Inflate (R.LAyout.recylce_item_y, null); Holer = new Buttonholder (v); Break;case bean.z_type:v = Layoutinflater.from ( Parent.getcontext ()). Inflate (r.layout.recylce_item_z, null); Holer = new Imageholer (v); break;} return holer;} /** * Bind Viewholder */@Overridepublic void Onbindviewholder (viewholder holder, int position) {//TODO auto-generated method Stubswitch (Getitemviewtype (position)) {case Bean.X_TYPE:TextHoler Textholer = (textholer) holder; Textholer.textView.setText (Beans.get (position). GetText ()); Break;case Bean.Y_TYPE:ButtonHolder Buttonholder = ( Buttonholder) Holder;buttonholder.button.settext (beans.get (position). GetText ()); Break;case bean.z_type:i Mageholer Imageholer = (imageholer) holder;//ImageHoler.Imageview.setImageResource (Android. R.drawable.checkbox_on_background); break;}}}
Finally, the activity code.
Package Com.androidl.bob;import Java.util.arraylist;import Java.util.list;import android.app.activity;import Android.os.bundle;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import Com.example.androidl.r;public class Mainactivity extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.main_activity); Recyclerview Mrecyclerview = (recyclerview) Findviewbyid (R.id.my_recycler_view);////Improve performance if you know That changes in content////does not change the size of the recyclerview//mrecyclerview.sethasfixedsize (true); Create layout manager Linearlayoutmanager Mlayoutmanager = new Linearlayoutmanager (this); Mlayoutmanager.setorientation ( linearlayoutmanager.vertical); Mrecyclerview.setlayoutmanager (Mlayoutmanager);//Initialize data list<bean> MyDataset = New Arraylist<bean> (); Mydataset.add (New Bean (Bean.z_type, "picture"); Mydataset.add(New Bean (Bean.x_type, "text")), Mydataset.add (New Bean (Bean.y_type, "button")), Mydataset.add (New Bean (Bean.z_type, "picture")); Mydataset.add (New Bean (Bean.x_type, "shit")), Mydataset.add (New Bean (Bean.x_type, "I Wipe")), Mydataset.add (New Bean ( Bean.z_type, "picture")); Mydataset.add (New Bean (Bean.y_type, "button")); Mydataset.add (New Bean (Bean.y_type, "button")); Mydataset.add (New Bean (Bean.x_type, "text"));//create Adapterrecycleadapter Madapter = new Recycleadapter (myDataSet); Mrecyclerview.setadapter (Madapter);}}
Demo Portal: Start transfer