java - Android inflate two layouts in recycler view adapter -
i trying inflate 2 different layouts in 1 recycler view. here adapter code.
import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import java.util.arraylist; public class homeadapter extends recyclerview.adapter<recyclerview.viewholder> { private arraylist<myhome> homedata; private static final int type_featured = 1; private static final int type_other = 2; public homeadapter(arraylist<myhome> mydataset) { this.homedata = mydataset; } public class featuredviewholder extends recyclerview.viewholder { public textview title; public textview author; public featuredviewholder(view itemview) { super(itemview); this.title = (textview)itemview.findviewbyid(r.id.title); this.author = (textview)itemview.findviewbyid(r.id.recvid_auth); } } public class othervidviewholder extends recyclerview.viewholder { public textview rec_title; public textview rec_author; public othervidviewholder(view itemview) { super(itemview); this.rec_title = (textview)itemview.findviewbyid(r.id.recvid_title); this.rec_author = (textview)itemview.findviewbyid(r.id.description); } } @override public int getitemviewtype(int position) { // example, return 0 or 2 depending on position // note unlike in listview adapters, types don't have contiguous return (position == 0? type_featured : type_other); } @override public recyclerview.viewholder oncreateviewholder(viewgroup viewgroup, int viewtype) { recyclerview.viewholder viewholder; layoutinflater inflater = layoutinflater.from(viewgroup.getcontext()); switch (viewtype) { case type_featured : view v1 = inflater.inflate(r.layout.layout_home_view, viewgroup, false); viewholder = new featuredviewholder(v1); //return new featuredviewholder(layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.layout_home_view, viewgroup, false)); break; case type_other : view v2 = inflater.inflate(r.layout.layout_home_other, viewgroup, false); viewholder = new othervidviewholder(v2); //return new othervidviewholder(layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.layout_home_other, viewgroup, false)); break; default: return null; } return viewholder; } @override public void onbindviewholder(recyclerview.viewholder viewholder, int i) { if(viewholder.getitemviewtype() == type_featured) { featuredviewholder featuredviewholder = (featuredviewholder) viewholder; featuredviewholder.title.settext(homedata.get(i).gettitle()); featuredviewholder.author.settext(homedata.get(i).getuser()); } else { othervidviewholder otherviewholder = (othervidviewholder) viewholder; otherviewholder.rec_title.settext(homedata.get(i).getviews()); } } @override public int getitemcount() { return homedata.size(); }
the code running shows 1 layout, other layout layout_home_other not showing. possible show 2 layouts? 2 layouts have different datas show. how retun size of second layout in getitemcount() also? thank get.
here dataset. public class myhome {
string title; string user; string views; string url; string duration; public myhome(string title, string user, string views, string url, string duration) { this.title = title; this.user = user; this.views = views; this.url = url; this.duration = duration; } public string gettitle() { return title; } public string getuser() { return user; } public string getviews() { return views; } public string geturl() { return url; } public string getduration() { return duration; } }
public class recyclerviewadapter extends recyclerview.adapter<recyclerview.viewholder> { // constructor public recyclerviewadapter(context context,"other parameters) { this.context = context; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view view ; switch (viewtype){ case -1: view = inflater.inflate(r.layout.layoutone, null, false); return new expiredviewholder(view); case 0: view = inflater.inflate(r.layout.layouttwo, null, false); return new activeviewholder(view); } } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { if (holder instanceof expiredviewholder) { ((expiredviewholder) holder).bind(tickerlist.get(position)); } else { if (holder instanceof activeviewholder) { ((activeviewholder) holder).bind(tickerlist.get(position)); } else { ((futureviewholder) holder).bind(tickerlist.get(position)); } } } @override public int getitemviewtype(int position) { return list.get(position).status; } @override public int getitemcount() { return list.size(); } static class activeviewholder extends recyclerview.viewholder { //your layout components public activeviewholder(view view) { super(view); //instantiate layout components fromyout view } public void bind(params) { //put text fields or components } } class expiredviewholder extends recyclerview.viewholder { public expiredviewholder(view view) { super(view); } public void bind(params) { } } static class futureviewholder extends recyclerview.viewholder { public futureviewholder( view view) { super(view); } public void bind(params) { } } }
public class mainclass{ recyclerview mrecyclerview ; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { mrecyclerview = (recyclerview) view.findviewbyid(r.id.recycler_view); mrecyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); adapter = new recyclerviewadapter(getactivity(), arrayticker); mrecyclerview.setadapter(adapter); return view; } }
Comments
Post a Comment