The Recyclerview and ListView principles are similar: they are only maintaining a small number of view and can display a large number of datasets. Recyclerview simplifies the presentation and processing of data in the following two ways:
Use LayoutManager to determine how each item is arranged.
Provides a default animation effect for adding and removing items.
Recyclerview Although as a replacement for ListView has a better performance improvement, but ListView some of the common functions are not provided, such as we often use the Addheaderview,addfooterview, Since Recyclerview does not provide this method, how should we add the head and bottom to the list? By looking at the source of ListView can know ListView Add header and footer is dynamically added by adapter inside, so we follow this idea also to recyclerview add Headerview and Footerview, First look at the effect
If you don't know how to use Recyclerview, you can look at the previous posts
The use of Recyclerview HelloWorld
Load of multiple item layouts used by Recyclerview
The core of the Recyclerview implementation add Headerview and Footerview is the Oncreateviewholder in adapter to determine whether a list item or a viewtype to load a different layout file separately , of course, ViewType's judgment rules are also defined by us, nonsense not to say, look at the specific implementation effect.
1:gradle Configuration Build.gradle
Compile ' com.android.support:recyclerview-v7:23.1.1 '
compile ' com.android.support:cardview-v7:23.1.1 '
2: Main layout file Activity_main.xml very simple inside a recyclerview
<?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.support.v7.widget.RecyclerView
xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "match_parent"
android:id= "@+id/rv_list"
/>
</LinearLayout>
3: List item layout rv_item.xml outside a CardView card-type container inside a TextView
<?xml version= "1.0" encoding= "Utf-8"?> <android.support.v7.widget.cardview xmlns:card_view=
" Http://schemas.android.com/apk/res-auto "
xmlns:android=" Http://schemas.android.com/apk/res/android
" 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:cardelevation= "4DP"
>
<linearlayout
android:layout_ Width= "Match_parent"
android:layout_height= "wrap_content" >
<textview
android:layout_width = "Wrap_content"
android:layout_height= "wrap_content"
android:id= "@+id/tv_item_text"
android: text= "Test"
android:layout_margin= "8DP"
/>
</LinearLayout>
</ Android.support.v7.widget.cardview>
4: List head layout rv_header.xml
<?xml version= "1.0" encoding= "Utf-8"?> <android.support.v7.widget.cardview xmlns:card_view=
" Http://schemas.android.com/apk/res-auto "
xmlns:android=" Http://schemas.android.com/apk/res/android
" 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:cardelevation= "4DP"
card_view:cardbackgroundcolor= "#4CAF50"
>
<linearlayout
android:layout_width= "match_parent"
android:layout_height= "150DP
" >
<textview
android:layout_width= "match_parent"
android:layout_height= "Match_parent"
android:text= "Header"
android:textsize= "30sp"
android:textcolor= "#ffffff"
android:gravity = "Center"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
5: List Bottom layout rv_footer.xml
<?xml version= "1.0" encoding= "Utf-8"?> xmlns:card_view= "Http://schemas.android.com/apk/res-auto" xmlns:android= "http://schemas.android.com/apk/res/
Android "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:cardelevation= "4DP" card_view:cardbackgroundcolor= "#E91E63" > <linearlayout android:layout_ Width= "Match_parent" android:layout_height= "150DP" > <textview android:layout_width= "match_parent" Android: layout_height= "Match_parent" android:text= "Footer" android:textsize= "30sp" android:textcolor= "#ffffff" Android: gravity= "center"/> </LinearLayout> </android.support.v7.widget.cardview>
6:* Headerbottomadapter.java,recyclerview's adapter, which determines the type of the current item in the Getitemviewtype method, then loads the different layouts in oncreateviewholder with the type of item to implement Headerview and Footerview, the meaning of other methods can refer to annotations
Import Android.content.Context;
Import Android.support.v7.widget.RecyclerView;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.TextView;
/** * Created by Lijizhou on 2016/2/24. * Blog:http://blog.csdn.net/leejizhou * qq:3107777777/public class Headerbottomadapter extends recyclerview.adapter& Lt recyclerview.viewholder> {//item type public static final int item_type_header = 0; public static final int Item_type_con
TENT = 1;
public static final int item_type_bottom = 2;
Analog data public String [] texts={"Java", "Python", "C + +", "Php", ". NET", "JS", "Ruby", "Swift", "OC"};
Private Layoutinflater Mlayoutinflater;
Private context Mcontext; private int mheadercount=1;//head view number private int mbottomcount=1;//Bottom view number public headerbottomadapter ) {Mcontext = context; mlayoutinflater = Layoutinflater.from;}//Content length public int getcontentitemcount () {return
Texts.length; //Determine if the current item is Headview public boolean IsHEaderview (int position) {return Mheadercount!= 0 && position < Mheadercount;}//Determine if the current item is Footerview public Boolean isbottomview (int position) {return Mbottomcount!= 0 && position >= (Mheadercount + Getcontentitemcou
NT ()); //Determine the current item type @Override public int getitemviewtype (int position) {int dataitemcount = Getcontentitemcount (); if (Mheader Count!= 0 && Position < Mheadercount) {//Head view return Item_type_header} else if (mbottomcount!= 0 &&A mp Position >= (Mheadercount + dataitemcount)) {//Bottom view return item_type_bottom;} else {//content view return Item_type_cont
ENT; }//Content Viewholder public static class Contentviewholder extends Recyclerview.viewholder {private TextView TextView; pub Lic Contentviewholder (View itemview) {super (Itemview); textview= (TextView) Itemview.findviewbyid (R.id.tv_item_text)
; }///header Viewholder public static class Headerviewholder extends Recyclerview.viewholder {public Headerviewholder (View it Emview) {supER (itemview); }//Bottom Viewholder public static class Bottomviewholder extends Recyclerview.viewholder {public Bottomviewholder (View it
Emview) {super (Itemview);}} @Override public Recyclerview.viewholder Oncreateviewholder (viewgroup parent, int viewtype) {if (ViewType ==item_type_ HEADER) {return new Headerviewholder (Mlayoutinflater.inflate (R.layout.rv_header, parent, false));} else if (ViewType = = Mheadercount) {return new Contentviewholder (Mlayoutinflater.inflate (R.layout.rv_item, parent, false));} else if (VIEWT
ype = = Item_type_bottom) {return new Bottomviewholder (Mlayoutinflater.inflate (R.layout.rv_footer, parent, false));}
return null; @Override public void Onbindviewholder (recyclerview.viewholder holder, int position) {if (Holder instanceof HEADERVIEWH Older) {} else if (holder instanceof Contentviewholder) {(contentviewholder) holder). Textview.settext (Texts[position-
Mheadercount]); else if (holder instanceof Bottomviewholder) {}} @Override public int getitemCount () {return mheadercount + getcontentitemcount () + Mbottomcount;}}
7: Last step, Mainactivity.java
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.support.v7.widget.GridLayoutManager;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView; public class Mainactivity extends Appcompatactivity {private Recyclerview mrecyclerview; private Headerbottomadapter Ada
Pter;
Gridlayoutmanager Gridlayoutmanager;
Linearlayoutmanager LayoutManager; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
mrecyclerview= (Recyclerview) Findviewbyid (r.id.rv_list);
List layout layoutmanager=new Linearlayoutmanager (this);
Layoutmanager.setorientation (linearlayoutmanager.vertical);
Mrecyclerview.setlayoutmanager (LayoutManager);
Mrecyclerview.setadapter (Adapter=new headerbottomadapter (this));
Grid layout//Gridlayoutmanager=new Gridlayoutmanager (Mainactivity.this, 2); Mrecyclerview.setlayoutmanager (Gridlayoutmanager)//Here with Linear Sudoku display similar to Grid VIEW//Mrecyclerview.setadapter (Adapter=new headerbottomadapter (this)); //Gridlayoutmanager.setspansizelookup (new Gridlayoutmanager.spansizelookup () {//@Override//public int Getspans ize (int position) {//return (Adapter.isheaderview (position) | | Adapter.isbottomview (position))?
Gridlayoutmanager.getspancount (): 1;
// }
// });
}
}
Note here that if your recyclerview uses the grid type list to call this method after setting the adapter, the current item type is used to determine the number of horizontal lattices occupied. And that's why adapter inside Isheaderview and Isbottomview.
Gridlayoutmanager.setspansizelookup (New Gridlayoutmanager.spansizelookup () {
@Override public
int getspansize (int position) {return
(Adapter.isheaderview (position) | | Adapter.isbottomview (position))? Gridlayoutmanager.getspancount (): 1;
}
});
Ok,recyclerview Add headers and footer is easy to achieve, you can also put the adapter encapsulation more conducive to the day-to-day development.
About the use of Recyclerview to add headers and footer to introduce to everyone here, I hope to help you!