android - i am trying to set a header to staggardGridLayoutManeger -


i trying set header staggardgridlayoutmaneger have code url https://github.com/willblaschko/header-footer-recycler-view. code contains header , footer types of layout. can set adapter staggardgridlayout code form sample at

enter code here public class recyclerviewheaderfooteradapter extends recyclerview.adapter {  public static final int type_manager_other = 0; public static final int type_manager_linear = 1; public static final int type_manager_grid = 2; public static final int type_manager_staggered_grid = 3;   public static final int type_header = 7898; public static final int type_footer = 7899;  private list<view> mheaders = new arraylist<>(); private list<view> mfooters = new arraylist<>();  private int mmanagertype; private recyclerview.layoutmanager mmanager; private irecyclerviewintermediary mintermediary;   public recyclerviewheaderfooteradapter(recyclerview.layoutmanager manager, irecyclerviewintermediary intermediary){     setmanager(manager);     this.mintermediary = intermediary; }  public void setlayoutmanager(recyclerview.layoutmanager manager){     setmanager(manager); }  private void setmanager(recyclerview.layoutmanager manager){     mmanager = manager;     if(mmanager instanceof gridlayoutmanager){         mmanagertype = type_manager_grid;         ((gridlayoutmanager) mmanager).setspansizelookup(mspansizelookup);     }else if(mmanager instanceof linearlayoutmanager){         mmanagertype = type_manager_linear;     }else if(mmanager instanceof staggeredgridlayoutmanager){         mmanagertype = type_manager_staggered_grid;         ((staggeredgridlayoutmanager) mmanager).setgapstrategy(staggeredgridlayoutmanager.gap_handling_move_items_between_spans);     }else{         mmanagertype = type_manager_other;     } }  public int getmanagertype(){     return mmanagertype; }  public int getgridspan(int position){     if(isheader(position) || isfooter(position)){         return getspan();     }     position -= mheaders.size();     if(mintermediary.getitem(position) instanceof igriditem){         return ((igriditem) mintermediary.getitem(position)).getgridspan();     }     return 1; }  private int getspan(){     if(mmanager instanceof gridlayoutmanager){         return ((gridlayoutmanager) mmanager).getspancount();     }else if(mmanager instanceof staggeredgridlayoutmanager){         return ((staggeredgridlayoutmanager) mmanager).getspancount();     }     return 1; }  private gridlayoutmanager.spansizelookup mspansizelookup = new gridlayoutmanager.spansizelookup(){     @override     public int getspansize(int position) {         return getgridspan(position);     } };   @override public recyclerview.viewholder oncreateviewholder(viewgroup viewgroup, int type) {     //if our position 1 of our items (this comes getitemviewtype(int position) below)     if(type !=  type_header && type != type_footer) {         return mintermediary.getviewholder(viewgroup, type);         //else have header/footer     }else{         //create new framelayout, or inflate resource         framelayout framelayout = new framelayout(viewgroup.getcontext());         //make sure fills space         framelayout.setlayoutparams(new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent));         return new headerfooterviewholder(framelayout);     } }  @override public void onbindviewholder(final recyclerview.viewholder vh, int position) {     //check type of view our position     if(isheader(position)){         view v = mheaders.get(position);         //add our view header view , display         prepareheaderfooter((headerfooterviewholder) vh, v);     }else if(isfooter(position)){         view v = mfooters.get(position-mintermediary.getitemcount()-mheaders.size());         //add our view footer view , display         prepareheaderfooter((headerfooterviewholder) vh, v);     }else {         //it's 1 of our items, display required         mintermediary.populateviewholder(vh, position-mheaders.size());     } }  private void prepareheaderfooter(headerfooterviewholder vh, view view){      //if it's staggered grid, span whole layout     if(mmanagertype == type_manager_staggered_grid){         staggeredgridlayoutmanager.layoutparams layoutparams = new staggeredgridlayoutmanager.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent);         layoutparams.setfullspan(true);         vh.itemview.setlayoutparams(layoutparams);     }      //if view belongs layout, remove     if(view.getparent() != null){         ((viewgroup)view.getparent()).removeview(view);     }      //empty out our framelayout , replace our header/footer     vh.base.removeallviews();     vh.base.addview(view);  }  private boolean isheader(int position){     return(position < mheaders.size()); } private boolean isfooter(int position){     return(position >= mheaders.size() + mintermediary.getitemcount()); }    @override public int getitemcount() {     return mheaders.size() + mintermediary.getitemcount() + mfooters.size(); }  @override public int getitemviewtype(int position) {     //check type our position is, based on assumption order headers > items > footers     if(isheader(position)){         return type_header;     }else if(isfooter(position)){         return type_footer;     }     int type = mintermediary.getitemviewtype(position);     if(type == type_header || type == type_footer){         throw new illegalargumentexception("item type cannot equal "+type_header+" or "+type_footer);     }     return type; }  //add header adapter public void addheader(view header){     if(!mheaders.contains(header)){         mheaders.add(header);         //animate         notifyiteminserted(mheaders.size()-1);     } }  //remove header adapter public void removeheader(view header){     if(mheaders.contains(header)){         //animate         notifyitemremoved(mheaders.indexof(header));         mheaders.remove(header);     } }  //add footer adapter public void addfooter(view footer){     if(!mfooters.contains(footer)){         mfooters.add(footer);         //animate         notifyiteminserted(mheaders.size()+mintermediary.getitemcount()+mfooters.size()-1);     } }  //remove footer adapter public void removefooter(view footer){     if(mfooters.contains(footer)) {         //animate         notifyitemremoved(mheaders.size()+mintermediary.getitemcount()+mfooters.indexof(footer));         mfooters.remove(footer);     } }  //our header/footer recyclerview.viewholder framelayout public static class headerfooterviewholder extends recyclerview.viewholder{     framelayout base;     public headerfooterviewholder(view itemview) {         super(itemview);         base = (framelayout) itemview;     } } 

}

i solved problem adding new type adapter header. @ example: https://stackoverflow.com/a/26573338/1949163.

after that, make header span columns created new layoutparams , set full span true in adapter implementation:

@override public void onbindviewholder(recyclerview.viewholder holder, int position) {     if (holder instanceof commonitemviewholder) {         ...     } else if (holder instanceof headerviewholder) {         ...         staggeredgridlayoutmanager.layoutparams layoutparams = new staggeredgridlayoutmanager.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent);         layoutparams.setfullspan(true);         holder.itemview.setlayoutparams(layoutparams);     } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -