android - Can I Use Only One RecyclerView For The Three Different CardView With Different Design? -
can create 1 recycler_view.xml
handle 3 set of cardview.xml
different design? or need create separate recyclerview
each 1 of cardviews
.
for example have different cardview
news, activities , groups
.
yes can use multiple cardview. reference see below link sample adapter
here showing list of reviews when no reviews displaying no reviews availble. here creating 2 viewholders 1 review item , 1 no review available item.
code:
package com.subbu.moviemasti.adapter; import android.support.v7.widget.recyclerview; import android.view.view; import android.view.viewgroup; import android.widget.textview; import com.subbu.moviemasti.r; import com.subbu.moviemasti.entities.review; import com.subbu.moviemasti.fragment.ireviewview; import java.util.list; import butterknife.bind; import butterknife.butterknife; /** * created subrahmanyam on 24-12-2015. */ public class reviewadapter extends recyclerview.adapter<recyclerview.viewholder> { private final list<review> results; public reviewadapter(ireviewview reviewfragment, list<review> results) { this.results = results; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = null; recyclerview.viewholder viewholder = null; switch (viewtype) { case 0: view = view.inflate(parent.getcontext(), r.layout.list_item_empty_view, null); viewholder = new emptyviewholder(view); break; case 1: view = view.inflate(parent.getcontext(), r.layout.review_list_item, null); viewholder = new viewholder(view); break; } return viewholder; } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { int viewtype = getitemviewtype(position); switch (viewtype) { case 0: emptyviewholder emptyviewholder = (emptyviewholder) holder; emptyviewholder.emptyview.settext(((emptyviewholder) holder).emptyview.getresources().getstring(r.string.no_trailers)); break; case 1: review review = results.get(position); viewholder viewholder = (viewholder) holder; viewholder.username.settext(review.getauthor()); viewholder.review.settext(review.getcontent()); break; } } @override public int getitemcount() { return results.size() > 0 ? results.size() : 1; } @override public int getitemviewtype(int position) { return results.size() == 0 ? 0 : 1; } class viewholder extends recyclerview.viewholder { @bind(r.id.review_user) textview username; @bind(r.id.review_review) textview review; public viewholder(view itemview) { super(itemview); butterknife.bind(this, itemview); } } }
Comments
Post a Comment