android - Recycleview contains child fragments -
hi guys want example of recycle view contains child fragments
, layout manager gridlayoutmanager
made 1 add in first cell , ignores others
public class simplefragment extends fragment {
private recyclerview mrecyclerview; private myadapter madapter; private recyclerview.layoutmanager mlayoutmanager; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_simple, container, false); } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); sethasoptionsmenu(true); mrecyclerview = (recyclerview) view.findviewbyid(r.id.my_recycler_view); mlayoutmanager = new gridlayoutmanager(getactivity(), 4); mrecyclerview.setlayoutmanager(mlayoutmanager); list<card> cards = new arraylist<>(); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.large, new fragmenthalf())); cards.add(new card(size.small, new fragmenthalf())); cards.add(new card(size.small, new fragmenthalf())); cards.add(new card(size.small, new fragmenthalf())); cards.add(new card(size.small, new fragmenthalf())); madapter = new myadapter(cards, mrecyclerview); mrecyclerview.setadapter(madapter); } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { super.oncreateoptionsmenu(menu, inflater); inflater.inflate(r.menu.menu_sample, menu); } @override public boolean onoptionsitemselected(menuitem item) { madapter.additem(5, new card(size.large, new fragmenthalf())); return super.onoptionsitemselected(item); } public class myadapter extends recyclerview.adapter<myadapter.viewholder> { private list<card> mdataset; private recyclerview recyclerview; private hashmap<fragment, integer> hashmapid = new hashmap<>(); // provide reference views each data item // complex data items may need more 1 view per item, , // provide access views data item in view holder public class viewholder extends recyclerview.viewholder { // each data item string in case public framelayout view; private int id; public viewholder(framelayout v) { super(v); this.view = v; id = v.getid(); } } // provide suitable constructor (depends on kind of dataset) public myadapter(final list<card> mydataset, recyclerview recyclerview) { mdataset = mydataset; this.recyclerview = recyclerview; gridlayoutmanager manager = (gridlayoutmanager) recyclerview.getlayoutmanager(); manager.setspansizelookup(new gridlayoutmanager.spansizelookup() { @override public int getspansize(int position) { return getitem(position).size == size.large ? 2 : 1; } }); } public card getitem(int pos) { return mdataset.get(pos); } public void additem(int position, card card) { if (position == -1) mdataset.add(card); else mdataset.add(position, card); notifyiteminserted(position); } // create new views (invoked layout manager) @override public myadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { framelayout v = (framelayout) layoutinflater.from(getactivity()).inflate(r.layout.my_text_view, parent, false); viewholder vh = new viewholder(v); return vh; } // replace contents of view (invoked layout manager) @override public void onbindviewholder(viewholder holder, int position) { // - element dataset @ position // - replace contents of view element card card = mdataset.get(position); getchildfragmentmanager().begintransaction().replace(holder.view.getid(), card.fragment).commit(); } // return size of dataset (invoked layout manager) @override public int getitemcount() { return mdataset.size(); } } enum size { large, small } class card { public size size; public fragment fragment; public int id; public card(size size, fragment fragment) { this.size = size; this.fragment = fragment; id = view.generateviewid(); } }
}
i solve mrecyclerview.setitemviewcachesize(cards.size());
and @ method @override public myadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) {
framelayout v = (framelayout) layoutinflater.from(getactivity()).inflate(r.layout.my_text_view, parent, false); v.setid(view.generateviewid());<-----added viewholder vh = new viewholder(v); return vh; }
Comments
Post a Comment