Recylerview set header and item from HasMap

32 Views Asked by At

expect result like this

expect result like that.. and the response from API like this enter image description here

then, how to implement it use HasMap and define header and item. please help me. thank you.

1

There are 1 best solutions below

0
On

I have implement it but there is issue, which is the footer is below of item. and this is my code:

public class HeaderItemYoutuberAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int HEADER = 1;
private static final int ITEM = 2;
private Context mContext;
List<ListItem> consolidatedList = new ArrayList<>();
private String guestName= "";
private String imgProfile = "";
private String descGuest= "";
private String posritionGuest= "";

public HeaderItemYoutuberAdapter(Context mContext, List<ListItem> consolidatedList) {
    this.consolidatedList = consolidatedList;
    this.mContext = mContext;
}



@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
    final View view;
    RecyclerView.ViewHolder viewHolder = null;

    if (type == ListItem.TYPE_HEADER) {
        View view1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.section_header, viewGroup, false);
        viewHolder = new HeaderViewHolder(view1);
    } else {
        View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.section_item, viewGroup, false);
        viewHolder = new ItemViewHolder(view2);

    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    if (viewHolder.getItemViewType() == ListItem.TYPE_HEADER) {
        HeaderItem generalItem   = (HeaderItem) consolidatedList.get(position);
        HeaderViewHolder generalViewHolder= (HeaderViewHolder) viewHolder;
        generalViewHolder.txtTitleHeader.setText(generalItem.getHeaderYotube().getCategory());

    } else {
        ItemContent dateItem = (ItemContent) consolidatedList.get(position);
        ItemViewHolder dateViewHolder = (ItemViewHolder) viewHolder;
        if (dateItem.getmStrings().size()>0){
            for (int i = 0; i<dateItem.getmStrings().size(); i++){
                guestName = dateItem.getmStrings().get(i).getGuestName();
                imgProfile = dateItem.getmStrings().get(i).getImgProfile();
                descGuest = dateItem.getmStrings().get(i).getDesc();
            }
        }

        dateViewHolder.txtTitle.setText(guestName);
        Glide.with(mContext)
                .load(imgProfile)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(mContext.getDrawable(R.drawable.image_placeholder))
                .fitCenter()
                .into(dateViewHolder.imgContent);
        dateViewHolder.txtDesc.setText(descGuest);        }
}

@Override
public int getItemCount() {
    return consolidatedList != null ? consolidatedList.size() : 0;
}


private boolean isPositionHeader(int position) {
    return position == 0;
}

@Override
public int getItemViewType(int position) {
   return consolidatedList.get(position).getType();

}

class HeaderViewHolder extends RecyclerView.ViewHolder {
    protected TextView txtTitleHeader;

    public HeaderViewHolder(View v) {
        super(v);
        this.txtTitleHeader = (TextView) v.findViewById(R.id.txt_header_section);

    }
}

class ItemViewHolder extends RecyclerView.ViewHolder {
    protected TextView txtTitle;
    protected ImageView imgContent;
    protected TextView txtDesc;

    public ItemViewHolder(View v) {
        super(v);
        this.txtTitle   = v.findViewById(R.id.txt_name_youtuber);
        this.txtDesc   = v.findViewById(R.id.txt_desc_youtuber);
        this.imgContent = v.findViewById(R.id.img_exhibition_item);

    }
}

and this is the result what i haveenter image description here