Advantages of Recyclerview:
- It brought Viewholder to realize the view of the reuse mechanism, no longer listview in GetView () to write their own
- Use LayoutManager to implement list effects for Listview,gridview and streaming layouts
- Through Setitemanimator (Itemanimator animator) can be implemented or deleted animation (lazy words, you can use the default Itemanimator object, the effect is good)
- Control the interval of the item, you can use Additemdecoration (itemdecoration decor), but inside the itemdecoration is an abstract class, you need to implement ...
Usage Introduction:
To import a Recyclerview V7 library:
Recyclerview is a control in a android.support.v7 library, so we need to add compile ' com.android.support:recyclerview-v7 to the Gradle configuration file when we use it. : 22.2.1 ' to introduce Google's official library
XML layout, the introduction of Recyclerview is introduced using a regular control approach, as follows:
<android.support.v7.widget.recyclerview
android:id= "@+id/recyclerview_content"
style= "? RecyclerView _style "
android:scrollbars=" vertical "
android:fadescrollbars=" true "
android:layout_width=" Match_ Parent "
android:layout_height=" match_parent "
android:layout_marginbottom=" -55DP "/>
The code is basically the same as ListView, but it is important to say:
After instantiating Recyclerview, we need to use Setlayoutmanager () to set up the layout manager, where the argument is LayoutManager, there are two LayoutManager in total:
1.StaggeredGridLayoutManager, is the streaming layout we mentioned before:
It has a construction method Staggeredgridlayoutmanager (int spancount, int orientation), the first is the number of columns in the grid, and the second parameter is the direction of the data rendering
(if it is vertical, then the first argument is the number of columns, otherwise the number of rows.) And the second parameter in the Staggeredgridlayoutmanager also have the same name of the constant, please accept the students themselves [here or suggest you use the library with the constant, because they are generally the integer value, This avoids other problems that can result from different constants that are judged in each class];
2.GridLayoutManager, Grid layout (a streaming layout should be a special case of it):
Gridlayoutmanager (context, int spancount) or
Gridlayoutmanager (context, int spancount, int Orientation,
boolean reverselayout) requires that the last parameter represents a reverse layout (meaning that the data is displayed in reverse, from left to right, from top to bottom). All reversed after set to true).
Tip: In these two layoutmanager, the default orientation is vertical,reverselayout to false. Corresponding parameters in the Gridlayoutmanager have a corresponding method to complement the settings. In Staggeredgridlayoutmanager, all methods are judged against reverselayout, however, it does not give an API for this parameter setting value.
Linear layout, Linearlayoutmanager
the Linearlayoutmanager (context context) constructs a recyclerview with a default layout direction of vertical, which is also listview default.
Linearlayoutmanager (context context, int orientation, Boolean reverselayout). Found no, the linear layout and grid layout are almost the same. Just one less spancount parameter.
As with ListView, set the adapter for Recyclerview.
class Homeadapter extends Recyclerview.adapter
As above, that is the adapter. One of the viewholder is to take responsibility for the View of the recovery and reuse, so that we do not need to write their own viewholder after, but also in getview (int position, View Convertview, ViewGroup Parent A meal to judge, a meal bound, a meal find. And the viewholder here becomes a part of the recyclerview that must be inherited, and after rewriting, you need to put recyclerview.adapter< > here to initialize the base class paradigm.
Here, Recyclerview has been packaged for you:
- GetItemCount () No need to say more, and ListView is the same
- Getitemviewtype (int position) is used to implement different layouts in Recyclerview according to the position. The value returned from this method is used in Oncreateviewholder. For example, the head, tail, and so on special itemview (this is said to be viewholder better) can be judged here.
- Oncreateviewholder (viewgroup parent, int viewtype) is used to match the written viewholder to return a Viewholder object. The loading of the item layout is also covered here. ViewType represents the type of viewholder that needs to be generated for the current position, and this parameter also illustrates Recyclerview support for multiple types of itemview.
- Onbindviewholder (myviewholder holder, int position) is specifically used to bind the controls in the Viewholder and the data in the position position in the data source.
Here, someone will ask, so where is the child control of item Findviewbyid? We give it to the Viewholder construction method (other methods also can), its essence is in Oncreateviewholder method to generate Viewholder when the execution.
Improve:
1. Code Refactoring:
look at the top of so many, there is wood feel, viewholder function is not very clear? It is responsible for both the subquery of the child control and the loading of the child control. and the layout loading and data binding are given to adapter ...
Let's take a look at the Nuggets ' approach:
First of all, it separates the functions of adapter and Viewholder in a relatively low coupling way, so that all the logic code in Viewholder only appears in Viewholder. We will now refactor the code mentioned earlier:
Viewhodler:
Class Myviewholder extends viewholder{
TextView TV;
Public Myviewholder (context, view view) {
super (view);
TV = (TextView) View.findviewbyid (r.id.id_num);
Of course, we can also use the view here to listen to an item on Recyclerview, or you can use
child controls such as//TV to implement event monitoring for the child control of item. This is one of the reasons why I want to pass the context ...
}
public static Myviewholder newinstance (activity context, ViewGroup parent) {
View view = Layoutinflater.from (
context). Inflate (R.layout.item_home, parent,false);
Return to new Myviewholder (context, view);
}
}
You're not mistaken, data binding is also integrated in,
//adapter data from the position to be sent in. Of course, can also be adjusted according to the specific circumstances. Public
void Onbinviewholder (String data) {
tv.settext (data);//Since there are child controls here, you can also do event sniffing for item child controls
}
Recyclerview:
Class Homeadapter extends recyclerview.adapter<myviewholder>{
@Override public
Myviewholder Oncreateviewholder (viewgroup parent, int viewtype) {//If necessary, make a judgment return to ViewType
myviewholder.newinstance (This, Parent)
}
@Override public
void Onbindviewholder (myviewholder holder, int position) {
Holder.onbindviewholder (Mdatas.get (position));
}
@Override public
int GetItemCount () {return
mdatas.size ();
}
}
Extract an entry Click event to make it more like ListView:
Class Homeadapter extends recyclerview.adapter<myviewholder>{private Onitemclicklistener Monitemclicklistener
; public void Setonitemclicklitener (Onitemclicklitener monitemclicklitener) {This.monitemclicklitener = MOnItemClickL
Itener; @Override public Myviewholder Oncreateviewholder (viewgroup parent, int viewtype) {//if needed, make a judgment return ViewType Ewholder.newinstance (this, parent)} @Override the public void Onbindviewholder (myviewholder holder, int position) {Hol
Der.onbindviewholder (Mdatas.get (position)); If a callback is set, set the Click event if (Monitemclicklitener!= null) {ViewHolder.itemView.setOnClickListener (The new Onclicklistener ( {@Override public void OnClick (View v) {Monitemclicklitener.onitemclick (Viewholder.itemview),
i);
}
});
@Override public int GetItemCount () {return mdatas.size ();
public interface Onitemclicklitener {void Onitemclick (view view, int position);
}
}
Interface calls
Madapter.setonitemclicklitener (New Onitemclicklitener () {
@Override public
void Onitemclick (view view, int Position) {
...
}
});
2.External
the above mentioned
Control the interval of the item, you can use Additemdecoration (itemdecoration decor), but inside the itemdecoration is an abstract class, you need to implement ...
This question, let's actually solve it:
This is a subclass of the goat God.
Package Com.zhy.sample.demo_recyclerview;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Canvas;
Import Android.graphics.Rect;
Import android.graphics.drawable.Drawable;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView;
Import Android.support.v7.widget.RecyclerView.State;
Import Android.util.Log;
Import Android.view.View;
public class Divideritemdecoration extends Recyclerview.itemdecoration {private static final int[] Attrs = new int[]{ Android.
R.attr.listdivider};
public static final int horizontal_list = linearlayoutmanager.horizontal;
public static final int vertical_list = linearlayoutmanager.vertical;
Private drawable Mdivider;
private int morientation; Public Divideritemdecoration (context context, int orientation) {Final TypedArray a = Context.obtainstyledattributes (a
TTRS);
Mdivider = a.getdrawable (0);
A.recycle (); SetOrientation (OrieNtation); public void setorientation (int orientation) {If orientation!= horizontal_list && Orientation!= vertic
Al_list) {throw new IllegalArgumentException ("Invalid orientation");
} morientation = orientation; @Override public void OnDraw (Canvas C, Recyclerview parent) {LOG.V ("recyclerview-itemdecoration", "OnDraw ()"
);
if (morientation = = vertical_list) {drawvertical (c, parent);
else {Drawhorizontal (c, parent);
} public void Drawvertical (Canvas C, Recyclerview parent) {Final int left = Parent.getpaddingleft ();
Final int right = Parent.getwidth ()-parent.getpaddingright ();
Final int childcount = Parent.getchildcount ();
for (int i = 0; i < ChildCount. i++) {final View child = Parent.getchildat (i);
Android.support.v7.widget.RecyclerView v = new Android.support.v7.widget.RecyclerView (Parent.getcontext ()); Final Recyclerview.layoutparams params = (recyclerview.
Layoutparams) Child getlayoutparams ();
Final int top = Child.getbottom () + Params.bottommargin;
final int bottom = top + mdivider.getintrinsicheight ();
Mdivider.setbounds (left, top, right, bottom);
Mdivider.draw (c);
} public void Drawhorizontal (Canvas C, Recyclerview parent) {Final int top = Parent.getpaddingtop ();
final int bottom = Parent.getheight ()-Parent.getpaddingbottom ();
Final int childcount = Parent.getchildcount ();
for (int i = 0; i < ChildCount. i++) {final View child = Parent.getchildat (i);
Final Recyclerview.layoutparams params = (recyclerview.layoutparams) child getlayoutparams ();
Final int left = Child.getright () + Params.rightmargin;
Final int right = left + mdivider.getintrinsicheight ();
Mdivider.setbounds (left, top, right, bottom);
Mdivider.draw (c); @Override public void Getitemoffsets (Rect outrect, int itemposition, Recyclerview parent) {if (morientation = = vertical_list) {outrect.set (0, 0, 0, mdivider.getintrinsicheight ());
else {outrect.set (0, 0, mdivider.getintrinsicwidth (), 0);
}
}
}
And then we add a split line for our Recyclerview instance.
Additemdecoration (This, divideritemdecoration.vertical_list) (new divideritemdecoration)
;
The system default segmentation line often can not reach our product and designer's request, how to do?
<item name= "Android:listdivider" > @drawable/your_custom_divider</item>
By using the XML attribute above, the system's Listdivider is set to the split line that you draw, just put it under the theme of your corresponding activity.
Tip: Show and hide the Recyclerview scroll bar
<android.support.v7.widget.recyclerview
android:layout_width= "match_parent"
android:layout_height= " Match_parent "
android:scrollbars=" vertical "
>
</android.support.v7.widget.RecyclerView>
Portrait Display:
android:scrollbars= "Vertical"
Landscape Display:
android:scrollbars= "Horizontal"
Hide:
Android:scrollbars= "None"