2

So I've manged to implement two headers into my recyclerview using a this method , but the way I have done it I believe to be in correct. For my two headers i have inflated two separate Layouts which is really unnecessary as I can use the layout for all the items in my recyclerview as well, also inflating layouts are expensive to do. How would i got about changing the text

This is my adapter code:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_HEADER2 = 11;
private static final int TYPE_ITEM = 1;
List<Information> data = Collections.emptyList();
LayoutInflater inflater;

public RecyclerViewAdapter(RecyclerViewTesting recyclerViewTesting, List<Information> data) {
    this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_HEADER) {
        return new VHHeader(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_custom_row_header_one, parent, false));
    }
    if (viewType == TYPE_HEADER2) {
        return new VHHeader(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_custom_row_header_two, parent, false));
    }

    return new VHItem(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_custom_row, parent, false));
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof VHItem) {
        Information item = getItem(position);
        ((VHItem) holder).title.setText(item.title);
    } else if (holder instanceof VHHeader) {
    }
}

@Override
public int getItemCount() {
    return data.size() + 1;
}

@Override
public int getItemViewType(int position) {
    if (isPositionHeader(position)){
        return TYPE_HEADER;
    }

    if (isPositionHeader2(position)){
        return TYPE_HEADER2;
    }

    return TYPE_ITEM;
}

private boolean isPositionHeader2(int position) { return position ==  11;
}

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

private Information getItem(int position) {
    return data.get(position - 1);
}

class VHItem extends RecyclerView.ViewHolder {
    TextView title;
    ImageView icon;

    public VHItem(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);
        icon = (ImageView) itemView.findViewById(R.id.listIcon);
    }
}

class VHHeader extends RecyclerView.ViewHolder {
    TextView title;
    ImageView icon;

    public VHHeader(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);
    }
}
}

Any Help would be hugely appreciated!

Community
  • 1
  • 1
Darragh O'Flaherty
  • 995
  • 3
  • 13
  • 30

0 Answers0