java - GridviewLayoutManager with headers -
i working on app
i have implemented working recycler view receives jsonarray, passes data string array.
i want add section headers layout manager.
i have read 2 schools of thought on this: - change spansize of view match total columns of grid - create custom adapter loads different view if item section header.
im not sure way go , starting confuse me
i have list of data in array includes both headers , grid data (mydataset), have created array mapping dataset in (mydatamap). in mydatamap have list of field types (1 header , 0 griddata. hoping pass both arrays adaptor , decide if header or griditem , load appropriate view.
i leaning more towards loading different view header items, allowing me customise layout of header easier.
here adaptor code
package com.example.alex.recyclerview2; import java.util.arraylist; import android.content.context; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; import com.squareup.picasso.picasso; public class myadapter extends recyclerview.adapter<myadapter.viewholder> { private arraylist<string> mdataset; private arraylist<integer> mdatamap; private context context; // 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 textview txtheader; public textview txtfooter; public imageview imgimage; public viewholder(view v) { super(v); txtheader = (textview) v.findviewbyid(r.id.firstline); txtfooter = (textview) v.findviewbyid(r.id.secondline); imgimage = (imageview) v.findviewbyid(r.id.icon); } } public void add(int position, string item) { mdataset.add(position, item); notifyiteminserted(position); } public void remove(string item) { int position = mdataset.indexof(item); mdataset.remove(position); notifyitemremoved(position); } // provide suitable constructor (depends on kind of dataset) public myadapter(arraylist<string> mydataset, arraylist<integer> mydatamap) {mdataset = mydataset;mydatamap=mdatamap; } // create new views (invoked layout manager) @override public myadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { // create new view view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.sub_layout, parent, false); context = v.getcontext(); // set view's size, margins, paddings , layout parameters 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 final string name = mdataset.get(position); picasso.with(context).load("http://www.500kgiveaway.co.uk/" + name).resize(200,200).into(holder.imgimage); // holder.txtheader.settext(mdataset.get(position)); holder.txtheader.settext(name); holder.txtheader.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { remove(name); } }); holder.txtfooter.settext("footer: " + mdataset.get(position)); } // return size of dataset (invoked layout manager) @override public int getitemcount() { return mdataset.size(); } }
ive not had interest in post here answer
i hope else
what have done implemented custom view adaptor manage item types
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.imageview; import android.widget.textview; import com.squareup.picasso.picasso; import java.util.arraylist; public class elementsadapter extends recyclerview.adapter<elementsadapter.viewholder> { private arraylist<string> mdataset; private arraylist<integer> mdatamap; public context context; private static final int view_header = 0; private static final int view_normal = 1; private view headerview; private int datasetsize; public class viewholder extends recyclerview.viewholder { // each data item string in case public textview txtheader; public textview txtfooter; public imageview imgimage; //header public textview headertext; public viewholder(view v, int viewtype) { super(v); switch (viewtype){ case 1: txtheader = (textview) v.findviewbyid(r.id.firstline); txtfooter = (textview) v.findviewbyid(r.id.secondline); imgimage = (imageview) v.findviewbyid(r.id.icon); case 0: headertext = (textview) v.findviewbyid(r.id.headertext); } } } public elementsadapter(arraylist<string> mydataset, arraylist<integer> mydatamap) { mdataset = mydataset; mdatamap = mydatamap; } @override public int getitemviewtype(int position) { return isheader(position) == 1 ? view_header : view_normal; } @override public int getitemcount() { return mdataset.size(); } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { if (viewtype == view_header) { // create new view view sub_view = layoutinflater.from(parent.getcontext()).inflate(r.layout.header, parent, false); context context = sub_view.getcontext(); // set view's size, margins, paddings , layout parameters viewholder vh = new viewholder(sub_view,viewtype); return vh; // return new headerviewholder(headerview); } else { // create new view view sub_view = layoutinflater.from(parent.getcontext()).inflate(r.layout.sub_layout, parent, false); context = sub_view.getcontext(); // set view's size, margins, paddings , layout parameters viewholder vh = new viewholder(sub_view, viewtype); return vh; } } @override public void onbindviewholder(viewholder viewholder, int position) { if (isheader(position) == 1) { // - element dataset @ position // - replace contents of view element final string name = mdataset.get(position); // holder.txtheader.settext(mdataset.get(position)); viewholder.headertext.settext(name); } else { // - element dataset @ position // - replace contents of view element final string name = mdataset.get(position); picasso.with(context).load("http://www.500kgiveaway.co.uk/"+name).resize(200,200).into(viewholder.imgimage); // holder.txtheader.settext(mdataset.get(position)); viewholder.txtheader.settext(name); viewholder.txtheader.setonclicklistener(new view.onclicklistener() { @override public void onclick (view v){ //remove(name); } } ); viewholder.txtfooter.settext("footer: "+mdataset.get(position)); } //viewholder holder = (viewholder) viewholder; //holder.textview.settext("position " + (position - 1)); } public int isheader(int position) { return mdatamap.get(position) ==1 ? 1:0;} }
Comments
Post a Comment