java - Android Implement AsymmetricGridView -
hello have problem implementation of assymetricgridview still crashing dont know wrong how implement correctly adapter there error link assymetricgridview library. thank you
adapter must instance of asymmetricgridviewadapter @ com.felipecsl.asymmetricgridview.library.widget.asymmetricgridview.setadapter(asymmetricgridview.java:71) @ com.gabriel.movies.fragment.mainfragment.render(mainfragment.java:140) @ com.gabriel.movies.fragment.mainfragment$mytask.onpostexecute(mainfragment.java:228) @ com.gabriel.movies.fragment.mainfragment$mytask.onpostexecute(mainfragment.java:161)
my code
<com.felipecsl.asymmetricgridview.library.widget.asymmetricgridview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview1" android:layout_width="match_parent" android:layout_height="match_parent" /> asymmetricgridview gridview; list<itemcategory> list; adaptercategory adapter; gridview = (asymmetricgridview) mrootview.findviewbyid(r.id.gridview1); gridview.setrequestedcolumnwidth(utils.dptopx(getactivity(), 120)); list = new arraylist<itemcategory>(); adapter = new adaptercategory(getactivity(), r.layout.fragment_worcipe_main_item, list); gridview.setadapter(adapter);
and adapter code
public class adaptercategory extends arrayadapter<itemcategory> { itemcategory object; private activity activity; private list<itemcategory> item; private int row; public adaptercategory(activity act, int resource, list<itemcategory> arraylist) { super(act, resource, arraylist); this.activity = act; this.row = resource; this.item = arraylist; } @override public view getview(final int position, view convertview, viewgroup parent) { view view = convertview; viewholder holder; if (view == null) { layoutinflater inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service); view = inflater.inflate(row, null); holder = new viewholder(); view.settag(holder); } else { holder = (viewholder) view.gettag(); } if ((item == null) || ((position + 1) > item.size())) return view; object = item.get(position); holder.title = (textview) view.findviewbyid(r.id.category_title); holder.image = (imageview) view.findviewbyid(r.id.category_image); holder.title.settext(object.getcategoryname()); picasso.with(getcontext()).load(recipeconfig.server_url + "/upload/" + object.getcategoryimageurl()).placeholder(r.drawable.ic_thumbnail).into(holder.image); return view; } public class viewholder { public textview title; public imageview image; } }
at first, need class, extends form asymmetricitem. should this:
public class demoitem implements asymmetricitem { private int columnspan; private int rowspan; private int position; private string text; public demoitem() { this(1, 1, 0,""); } public demoitem(int columnspan, int rowspan, int position,string text) { this.columnspan = columnspan; this.rowspan = rowspan; this.position = position; this.text=text; } public demoitem(parcel in) { readfromparcel(in); } @override public int getcolumnspan() { return columnspan; } @override public int getrowspan() { return rowspan; } public string gettext(){ return text; } public int getposition() { return position; } @override public string tostring() { return string.format("%s: %sx%s", position, rowspan, columnspan); } @override public int describecontents() { return 0; } private void readfromparcel(parcel in) { columnspan = in.readint(); rowspan = in.readint(); position = in.readint(); } @override public void writetoparcel(@nonnull parcel dest, int flags) { dest.writeint(columnspan); dest.writeint(rowspan); dest.writeint(position); } /* parcelable interface implementation */ public static final parcelable.creator<demoitem> creator = new parcelable.creator<demoitem>() { @override public demoitem createfromparcel(@nonnull parcel in) { return new demoitem(in); } @override @nonnull public demoitem[] newarray(int size) { return new demoitem[size]; } }; }
then need create adapter, should this(exept should recycle views):
public class gridadapter extends arrayadapter<demoitem> { private final layoutinflater layoutinflater; public gridadapter(context context, list<demoitem> items) { super(context, 0, items); layoutinflater = layoutinflater.from(context); } public gridadapter(context context) { super(context, 0); layoutinflater = layoutinflater.from(context); } @override public view getview(int position, view convertview, viewgroup parent) { demoitem item = getitem(position); view v = layoutinflater.inflate( r.layout.adapter_item, parent, false); textview textview = (textview) v.findviewbyid(r.id.textview); textview.settext(item.gettext()); return v; } @override public int getviewtypecount() { return 2; } @override public int getitemviewtype(int position) { return position % 2 == 0 ? 1 : 0; } public void appenditems(list<demoitem> newitems) { addall(newitems); notifydatasetchanged(); } public void setitems(list<demoitem> moreitems) { clear(); appenditems(moreitems); } }
and finally, when have of that, can implement in activity/fragment this:
private listadapter adapter; private asymmetricgridview listview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.dashboard_gridview, container, false); listview = (asymmetricgridview) rootview.findviewbyid(r.id.listview); listview.setallowreordering(true); final list<demoitem> items = new arraylist<>(); items.add(new demoitem(2,2,0,"example text 1")); items.add(new demoitem(1,1,0,"example text 2")); items.add(new demoitem(1,1,0,"example text 3")); items.add(new demoitem(2,2,0,"example text 4")); items.add(new demoitem(1,1,0,"example text 5")); items.add(new demoitem(1,1,0,"example text 6")); items.add(new demoitem(1,1,0,"example text 7")); items.add(new demoitem(2,2,0,"example text 8")); items.add(new demoitem(1,1,0,"example text 9")); items.add(new demoitem(1,1,0,"example text 10")); adapter = new gridadapter(getcontext(), items); listview.setadapter(new asymmetricgridviewadapter(getcontext(), listview, adapter)); return rootview; } }
and that's it, hope you.
Comments
Post a Comment