Recycleview
Brief introduction:
Recylerview is a new component in the SUPPORT-V7 package and is a powerful sliding component that also has the ability to recycle the item as compared to the classic ListView, as can be seen from its name Recylerview, which is the recycle view.
Advantages and Effects:
According to the official introduction Recylerview is a ListView upgrade version, so that Recylerview must have its advantages, is now recylerview relative to the advantages of the ListView listed as follows:
- Recylerview encapsulates the Viewholder recovery reuse, which means recylerview standardized viewholder, writing adapter for Viewholder instead of view, the logic of reuse is encapsulated and easier to write.
- Provides a plug-and-play experience, a high degree of decoupling, abnormal flexibility, for an item display Recylerview specifically extracted the corresponding class, to control the display of the item, so that its extensibility is very strong. For example: you want to control the landscape (which solves the problem that the ListView can only be distributed vertically) or the vertical slide list effect can be controlled by the Linearlayoutmanager class (which corresponds to the GridView effect is Gridlayoutmanager, and waterfall flow corresponds to Staggeredgridlayoutmanager and so on, that is to say Recylerview no longer adhere to the ListView of the linear display, it can also achieve the effect of the GridView and many other effects. You want to control the divider for item, you can inherit Recylerview's Itemdecoration class, and then describe the code for your business needs.
- Can control the item additions and deletions animation, can be controlled by itemanimator this class, of course, for the animation of additions and deletions, Recylerview has its own default implementation.
Preliminary use of Recycleview
First, the first step is to import the V7 package
Right click on the project and open Module Settings
Go to the following screen and click Dependencies
Click the + symbol in the lower left corner
Click on the first library dependency, go to the following interface to select the RECYCLEVIEW-V7 package on the line
Acvitity_main.xml layout:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:app=" Http://schemas.android.com/apk/res-auto " xmlns:tools=" http://schemas.android.com/ Tools " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" tools:context= "com.contentprovide.liuliu.recycle_3.MainActivity" > < TextView android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= " The following is Recycleview " /> <android.support.v7.widget.recyclerview android:id=" @+id/myrecycle " android:layout_width= "wrap_content" android:layout_height= "200DP" ></ Android.support.v7.widget.recyclerview></linearlayout>
Java Code Implementation:
Package Com.contentprovide.liuliu.recycle_3;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import Android.view.view;import Android.view.viewgroup;import android.widget.textview;/*** add Recycleview to the layout file * */public class Mainactivity extends Appcompatactivity {//Declare a RE Cycleview variable Recyclerview recyclerview; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//Bind the custom Recycleview variable with the ID in activity_main.xml Recyclerview = (Recycle Rview) Findviewbyid (r.id.myrecycle);//Set the layout of the Recycleview, here is the linear layout, the default vertical recyclerview.setlayoutmanager (new Lin Earlayoutmanager (this));//Instantiate custom adapter Myadapter myadapter = new Myadapter ();//Add Adapter to Recycleview Recyclerview.setadapter (Myadapter); }//Custom class inheritance RECYCLEVIEW.AThe Dapter class acts as the data adapter class Myadapter extends Recyclerview.adapter {////In the adapter to customize the inner class, where the child objects are used to render the data class M Yholder extends Recyclerview.viewholder {TextView t; Public Myholder (view view) {super view;//Instantiate custom Object t = (TextView) View; }}//The following are three methods that are automatically generated by the system @Override public Recyclerview.viewholder Oncreateviewholder (View Group parent, int viewtype) {Myholder Myholder = new Myholder (New TextView (Parent.getcontext ())); return myholder; }//Assign a value to the control @Override public void Onbindviewholder (recyclerview.viewholder holder, int position ) {Myholder mm = (myholder) holder; Mm.t.settext ("item" + position); } @Override public int getitemcount () {return 10; } }}
Implementation Results :
You can see the setup process for the Recylerview, which is more complicated than the ListView, which is the performance of the Recylerview highly decoupled, although the code is somewhat complex, but its extensibility is extremely high. After understanding some of the Recyclerview control, and then look at its adapter, Recyclerview adapter and the adapter of the ListView is a little different, Recyclerview.adapter, there are 3 methods that need to be implemented: ①oncreateviewholder () This method is primarily created for each item Inflater a view, but the method returns a Viewholder. This method encapsulates the view directly in the Viewholder, and then we are looking at the example of Viewholder, and of course this viewholder needs to be written by ourselves. Directly omitted from the original Convertview.settag (holder) and Convertview.gettag () these tedious steps. ②onbindviewholder () This method is mainly used to fit the rendering data into the view. The method provides you with a viewholder, not the original Convertview. ③getitemcount () This method is similar to the Baseadapter GetCount method, that is, the total number of entries.
Customizing list items with resource files
A lot of times each list item we need to show more content, and to make it more beautiful, this time we need to customize the list item, what kind of effect you need to define yourself in a new XML resource file
For example, the following effects are achieved:
Steps:
Custom list item layout file List.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" vertical "> <textview Android:id= "@+id/te1" android:layout_width= "match_parent" android:layout_height= "Wrap_content " Android:textcolor= "#e20707" android:textsize= "30DP"/> <textview android:id= "@+id/te2" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:textcolor= "# 0FEC1A " android:textsize=" 15DP "/></linearlayout>
The same Activity_main.xml file as above:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:app=" Http://schemas.android.com/apk/res-auto " xmlns:tools=" http://schemas.android.com/ Tools " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" tools:context= "com.contentprovide.liuliu.recycle_4.MainActivity" > < TextView android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= " The results are as follows: "/> <android.support.v7.widget.recyclerview android:id=" @+id/myrecycleview " Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" ></ Android.support.v7.widget.recyclerview></linearlayout>
Java Code Implementation:
Package Com.contentprovide.liuliu.recycle_4;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import Android.widget.textview;public class Mainactivity extends Appcompatactivity { Recyclerview Recyclerview; String s_te1[] = {"Content one", "Content One", "Content One"}; String s_te2[] = {"Content II", "Content II", "Content II"}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Recyclerview = (Recyclerview) Findviewbyid (R.id.myrecycleview); Recyclerview.setlayoutmanager (New Linearlayoutmanager (this)); Myadapter myadapter = new Myadapter (); Recyclerview.setadapter (Myadapter); } class Myadapter extends Recyclerview.adapter {@Override public recyclerview.viewhoLder Oncreateviewholder (viewgroup parent, int viewtype) {Myholder Myholder = new Myholder ( Layoutinflater.from (Getapplicationcontext ()). Inflate (r.layout.list, NULL))///Import the resource file of the custom list item return myholder; } @Override public void Onbindviewholder (recyclerview.viewholder holder, int position) {Myholder mm = (Myholder) holder;//maps the data to the control Mm.te1.setText (S_te1[position]); Mm.te2.setText (S_te2[position]); } @Override public int getitemcount () {return s_te1.length; } class Myholder extends Recyclerview.viewholder {TextView te1, te2; Public Myholder (View Itemview) {super (Itemview);//Instantiate sub-object, bind object and ID in list item layout file Te1 = Itemview.findviewbyid (r.id.te1); TE2 = Itemview.findviewbyid (R.ID.TE2); } } }}
Here the basic use of Recycleview is done, the following add some more flexible use, for example: increase the divider line, through a few lines of code changes into the GridView effect. This is where recycleview can be more flexible than the ListView.
Change the direction of the linear layout from vertical to horizontal to achieve the following effects:
Recyclerview.setlayoutmanager (new Linearlayoutmanager (This,linearlayoutmanager.horizontal,false));
You can see not only the horizontal layout, but also horizontal scrolling, which is not supported in the ListView
Change the linear layout to the GridView layout
Recyclerview.setlayoutmanager (New Gridlayoutmanager (this,4));
The grid layout is vertical by default, and if it is vertical then the second parameter represents how many columns, and if it is a horizontal layout then the second parameter represents how many rows
Convert to Horizontal layout:
Recyclerview.setlayoutmanager (New Gridlayoutmanager (This,4,gridlayoutmanager.horizontal,false));
To add a split line that comes with your system:
Recyclerview.additemdecoration (New Divideritemdecoration (this,divideritemdecoration. VERTICAL));
Custom split lines are tedious and interesting to look at this great God's
http://blog.csdn.net/dmk877/article/details/50816933
Android Development Recycleview Use full solution