Prerequisite Summary:
The previous article has introduced the basic use method of Recyclerview, the original text is as follows: Android Recyclerview layout really just so simple! This article is an introduction to the deeper use of Recyclerview.
Footview and Headview have corresponding functions in their ListView, but not in the trendy Recyclerview, Footview play an important role in paging loading (more on the pull), and therefore must be studied. (the addition of Headview is roughly the same as the addition of Footview, this is only about Footview additions)
Effect: (source code at the end of the article)
Achieve key
int getitemviewtype (int position): This function is a function in Recyclerview, the parameter is the position of each item, and returns an int type representation type.
This function is to distinguish between ordinary item and Footview item, let Footview this item can always be at the bottom of adapter.
Two types are defined in the example:
The two final int type represents the two types of ViewType
private final int normal_type = 0;
Private final int foot_type = 1111;
The function is as follows:
@Override public
int getitemviewtype (int position) {
if (position = = max_count-1) {return
foot_type;
} return
normal_type;
}
Implementation steps
1, define GETITEMVIEWTYPE (int position) and define the type of viewtype you need.
2, in the definition of Viewholder (), Oncreateviewholder () and Onbindviewholder () are considered in two cases, one is the ordinary item, the other is Footview.
In addition, the default Viewholder () function will only have a view Itemview a parameter, which is needed, so to add the parameters of the int viewtype, the code is as follows:
Initializes Viewholder, where the onbindviewholder can be used directly in the public
Viewholder (View itemview, int viewtype) {
super ( Itemview);
if (ViewType = = Normal_type) {
Tvviewholder = (TextView) Itemview.findviewbyid (r.id.tv_view_holder);
Llviewholder = (linearlayout) Itemview;
} else if (ViewType = = Foot_type) {
Tvfootview = (TextView) itemview;
}
}
Here's the general effect of the three functions:
Viewholder (View itemview, int viewtype):
The control in item layout is bound to the property defined in Viewholder and is easier to use in Onbindviewholder ().
Oncreateviewholder (viewgroup parent, int viewtype):
This function is used to create each item, and the last return is not view, but a viewholder returned.
Onbindviewholder (viewholder holder, int position):
This function is typically used to bind data to a control in item.
Code:
In combination with the above analysis to see the code, readers should be more easily understood, the following attach key adapter code and source address:
public class Recycleradapter extends recyclerview.adapter<recycleradapter.viewholder> {private list<string& Gt mdata;//data private int max_count = 10;//Maximum display number private Boolean Isfootview = false;//whether Footview private String Footvie was added
Wtext = "";//footview content//two final int type represents viewtype two types of private final int normal_type = 0;
Private final int foot_type = 1111;
Public Recycleradapter (list<string> data) {mdata = data;
public class Viewholder extends Recyclerview.viewholder {public TextView tvviewholder;
Public LinearLayout Llviewholder; The TextView of public TextView Tvfootview;//footview belongs to a single layout//initialization viewholder, where you can directly use public in onbindviewholder after binding
Viewholder (View itemview, int viewtype) {super (Itemview);
if (ViewType = = normal_type) {Tvviewholder = (TextView) Itemview.findviewbyid (R.id.tv_view_holder);
Llviewholder = (linearlayout) Itemview;
else if (ViewType = = foot_type) {Tvfootview = (TextView) Itemview; "}}} @Override public ViewHolder Oncreateviewholder (viewgroup parent, int viewtype) {View normal_views = Layoutinflater.from (Parent.getcontext ()
). Inflate (R.layout.rc_item, parent, false);
View Foot_view = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.foot_view, parent, false);
if (ViewType = = Foot_type) return to New Viewholder (Foot_view, Foot_type);
return new Viewholder (Normal_views, Normal_type);
@Override public int Getitemviewtype (int position) {if (position = max_count-1) {return foot_type;
return normal_type; @Override public void Onbindviewholder (viewholder holder, int position) {//establishes an attempt to associate with the data in Viewholder ("LOG.D", get
Itemviewtype (position) + "");
If Footview exists, and the current position ViewType is Foot_type if (Isfootview && (getitemviewtype (position) = Foot_type)) {
Holder.tvFootView.setText (Footviewtext);
else {Holder.tvViewHolder.setText (Mdata.get (position) + position); @Override public int GetItemCount () {if (Mdata.size () < Max_count) {RetuRN Mdata.size ();
return max_count;
///Create a method to set the text in Footview to public void Setfootviewtext (String footviewtext) {Isfootview = true;
This.footviewtext = Footviewtext;
}
}
Expand extension
In the next article, the author made a little change on this basis, it is easy to implement the paging load function, the article address is as follows:
http://blog.csdn.net/double2hao/article/details/52788708
SOURCE Address: Http://xiazai.jb51.net/201610/yuanma/AndroidPullRecyclerView (jb51.net). rar
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.