0

I have a RecycleView which contains fixed number of CardView (14 in my case) and some of these cards may be one of two types layouts for cardView. And.. my question is about how can I implement this in code?

I have class for a RecycleItem:

public class RecyclerItem {

private String title;
private String description;

public RecyclerItem(String _title, String _description, boolean _weekEnd) {
    this.title = _title;
    this.description = _description;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public void getWeekEnd(boolean _weekEnd){
    this.weekEnd = _weekEnd;
}

public boolean getWeekEnd(){
    return this.weekEnd;
}

}

and class for Adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<RecyclerItem> listItems;
private Context mContext;

public MyAdapter(List<RecyclerItem> listItems, Context mContext) {
    this.listItems = listItems;
    this.mContext = mContext;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView title, description;
    public ViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.titleOfTheCard);
        description = (TextView) itemView.findViewById(R.id.descriptionOfTheCard);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false));
}

@Override
public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
    final RecyclerItem itemList = listItems.get(position);
    holder.title.setText(itemList.getTitle());
    holder.description.setText(itemList.getDescription());
}

@Override
public int getItemCount() {
    return listItems.size();
}

}

I just don't know how to change 'type' of c card while we filling the recycleView items list and how fill it correctly, so can you help me with that??

And one another question. Do you know good literature about developing apps for android?

  • not understand your question because type not field in your pojo class.because every things written are correct. –  Apr 06 '18 at 06:46
  • 1
    implement getItemViewType in your adapter and inflate your views based on view type in OnCreateViewHolder @Override public int getItemViewType(int position) { return chatModelList.get(position).isMyMessage() ? ITEM_VIEW_OUTGOING : ITEM_VIEW_INCOMING; } – hasan_shaikh Apr 06 '18 at 07:00

0 Answers0