Android Recyclerview Add head and bottom method _android

Source: Internet
Author: User
Tags static class

If just want to add head, but use GitHub inside this item, it can be Linearlayoutmanager,gridlayoutmanager, Add headers to the Recyclerview of the Staggeredgridlayoutmanager layout. It is also very simple to use;
Simply place the Recyclerviewheader layout on top of the Recyclerview.

<framelayout
 android:layout_width= "match_parent"
 android:layout_height= "wrap_content" >

 < Android.support.v7.widget.RecyclerView
  android:id= "@+id/recycler"
  android:layout_width= "Match_parent"
  android:layout_height= "wrap_content"
  android:layout_gravity= "Center_horizontal|top"/>

 < Com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader
  android:id= "@+id/header"
  android:layout_ Width= "Match_parent"
  android:layout_height= "100DP"
  android:layout_gravity= "Center_horizontal|top" >

  <textview
   android:layout_width= "wrap_content"
   android:layout_height= "Wrap_content"
   android:layout_centerinparent= "true"
   android:text= "header"/>

 </ Com.bartoszlipinski.recyclerviewheader.recyclerviewheader>

</FrameLayout>

Then get the Recyclerviewheader object:

Recyclerviewheader Header = (recyclerviewheader) Findviewbyid (R.id.header);

Give the Recyclerviewheader to Recyclerview.

Recyclerview Recyclerview = (recyclerview) Findviewbyid (R.id.recycler_view);
Set LayoutManager for your Recyclerview
Header.attachto (Recyclerview, true);

Attention matters
Recyclerviewheader must be called after the Recyclerview has been set LayoutManager.

The library is currently applicable to the recyclerviews of Linearlayoutmanager,staggeredgridlayoutmanager and gridlayoutmanager layouts. Only vertical layout LayoutManager is supported. If you intend to use Setonscrolllistener (...) in Recyclerview. method to ensure that the Setonscrolllistener (...) Attachto (...) Method before using the.

Of course, we can also write a recyclerview to add the head and bottom. The basic principle of its implementation is to add the head and the bottom by returning different types through the Getitemviewtype method.
First we customize a recyclerview:

public class Wraprecyclerview extends Recyclerview {public arraylist<view> mheaderviews = new Arraylist<> ()
 ;
 Public arraylist<view> mfooterviews = new arraylist<> ();
 Add Adapter public Adapter madapter;
 Public Wraprecyclerview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
 Public Wraprecyclerview (context, AttributeSet attrs) {Super (context, attrs);
 Public Wraprecyclerview {Super (context);
  public void Addheaderview (view view) {mheaderviews.clear ();
  Mheaderviews.add (view); if (madapter!=null) {if (!) (
   Madapter instanceof Recyclerwrapadapter)) {madapter = new Recyclerwrapadapter (mheaderviews,mfooterviews,madapter);
  }} public void Addfooterview (view view) {mfooterviews.clear ();
  Mfooterviews.add (view); if (madapter!=null) {if (!) (
   Madapter instanceof Recyclerwrapadapter)) {madapter = new Recyclerwrapadapter (mheaderviews,mfooterviews,madapter); }
  } public void Setadapter (Adapter Adapter) {if (Mheaderviews.isempty () &&mfooterviews.isempty ()) {Super
  . Setadapter (adapter);
   }else {adapter = new Recyclerwrapadapter (mheaderviews,mfooterviews,adapter);
  Super.setadapter (adapter);
 } Madapter = adapter;

 }
}

We'll see that we have a recyclerwrapadapter not implemented, so let's look at the Recyclerwrapadapter, which is the key to adding the head and tail.

public class Recyclerwrapadapter extends Recyclerview.adapter implements wrapperadapter{private Recyclerview.adapter m

 Adapter;

 Private arraylist<view> mheaderviews;
 Private arraylist<view> mfootviews;
 Static final arraylist<view> empty_info_list = new arraylist<view> ();
 private int mcurrentposition; Public Recyclerwrapadapter (arraylist<view> mheaderviews, arraylist<view> mfootviews,
  Recyclerview.adapter madapter) {this.madapter = Madapter;
  if (mheaderviews = = null) {this.mheaderviews = empty_info_list;
  else {this.mheaderviews = mheaderviews;
  } if (mfootviews = = null) {this.mfootviews = empty_info_list;
  else {this.mfootviews = mfootviews;
 } public int Getheaderscount () {return mheaderviews.size ();
 public int Getfooterscount () {return mfootviews.size (); @Override public Recyclerview.viewholder Oncreateviewholder (viewgroup parent, int viewtype) {if (ViewType = = Recycl erview.invalid_type) {return new Headerviewholder (Mheaderviews.get (0));
  else if (ViewType = = recyclerview.invalid_type-1) {return new Headerviewholder (Mfootviews.get (0));
 Return Madapter.oncreateviewholder (parent, ViewType); 
  @Override public void Onbindviewholder (recyclerview.viewholder holder, int position) {//If the head is not empty, then we need to add the head first, so we just Give the front several position to the head, and when the position is smaller than the head count,//We return to the head view.
  Then judge the original Adapter count and the current position//difference to compare, is to call the original Adapter GetView method, or get Footview//view.
  int numheaders = Getheaderscount ();
  if (position < numheaders) {return;
  int adjposition = position-numheaders;
  int adaptercount = 0;
   if (madapter!= null) {Adaptercount = Madapter.getitemcount ();
    if (Adjposition < Adaptercount) {Madapter.onbindviewholder (holder, adjposition);
   Return @Override public int GetItemCount () {if (Madapter!= null) {return getheaderscount () + Getfooterscount (
  ) + Madapter.getitemcount ();
} else {   return Getheaderscount () + Getfooterscount ();
 @Override public Recyclerview.adapter Getwrappedadapter () {return madapter; @Override public int Getitemviewtype (int position) {//Increase two types//recyclerview.invalid_type add head//recyclerview.in valid_type-1 Add tail//If the head is not empty, then we need to add the head first, so we just//put the front several position to the head, when position is less than the head total,//We return the head type.
  To judge the difference between the count of the original Adapter and the current position//, is to call the type of the original Adapter, or to obtain the Footview//.
  Mcurrentposition = position;
  int numheaders = Getheaderscount ();
  if (position<numheaders) {return recyclerview.invalid_type;
  int adjposition = position-numheaders;
  int adaptercount = 0;
   if (madapter!=null) {adaptercount = Madapter.getitemcount ();
   if (Adjposition < Adaptercount) {return madapter.getitemviewtype (adjposition);
 } return recyclerview.invalid_type-1; } private static Class Headerviewholder extends Recyclerview.viewholder {public headerviewholder (View Itemview) {s Uper (ITEMVIEW);
 }
 }
}

We can also implement an interface to invoke the Recyclerwrapadapter object:

Public interface Wrapperadapter {public

 recyclerview.adapter getwrappedadapter ();
}

So we can change the Recyclerview layout to Wraprecyclerview, and then call Addheaderview or Addfooterview can add the head and tail.

This is the entire content of this article, I hope to learn more about Android software programming help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.