I would use the same RecyclerView.Adapter with two or more different fragments. Every fragment uses a different view items layout so I must use a different RecyclerView.ViewHolder for binding the data.

For the implementation I have created one RecycleView.ViewHolder that binds the two different view layout with a switch but I think might be something better for this kind of situation.

public class TFViewHolder extends RecyclerView.ViewHolder  {

    public final static int LAYOUT_ONE = 1;
    public final static int LAYOUT_TWO = 2;

    public Integer mId;

    public ImageView mThumb;
    public TextView mName;

    public TextView mTitle;
    public TextView mDescription;

    public final View mView;

    public TFViewHolder(View itemView, int layoutType) {

        mView = itemView;

        switch (layoutType) {

            case LAYOUT_ONE:
                mThumb = (ImageView) itemView.findViewById(R.id.thumb);
                mName = (TextView) itemView.findViewById(R.id.name);
            case LAYOUT_TWO:
                mTitle = (TextView) itemView.findViewById(R.id.title);
                mDescription = (TextView) itemView.findViewById(R.id.description);


The RecyclerView.Adapter:

public class TFRecyclerViewAdapter extends RecyclerView.Adapter<TFViewHolder> {

    protected Context context;
    protected List items;
    private int layout;
    private int layoutType;

    public TFRecyclerViewAdapter(Context context) {
        this.context = context;

    public TFRecyclerViewAdapter(Context context, int layout, int layoutType) {

        this.layout = layout;
        this.layoutType = layoutType;

    public void setItems(List items){
        this.items = items;

    public void onBindViewHolder(final TFViewHolder holder, final int position) {
        holder.mId = position;

    public TFViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(layout, parent, false);

        return new TFViewHolder(itemView, layoutType);

    public int getItemCount() {
        int l = 0;

        if (items != null) {
            l = items.size();

        return l;

Two different adapters that extend the TFRecyclerViewAdapter of above:

public class TFNewsRecyclerViewAdapter extends TFRecyclerViewAdapter {

    public TFNewsRecyclerViewAdapter(Context context, int layout) {
        super(context, layout, TFViewHolder.LAYOUT_NEWS);

    public void onBindViewHolder(TFViewHolder holder, int position) {

        final News item = (News) items.get(position);


        super.onBindViewHolder(holder, position);


public class TFTeamsRecyclerViewAdapter extends TFRecyclerViewAdapter {

    public TFTeamsRecyclerViewAdapter(Context context, int layout) {
        super(context, layout, TFViewHolder.LAYOUT_TEAMS);

    public void onBindViewHolder(TFViewHolder holder, int position) {

        final Team team = (Team) items.get(position);



        super.onBindViewHolder(holder, position);
  • \$\begingroup\$ Commenting because I haven't had time to look too closely, but instead of switching inside the ViewHolder you could create multiple ViewHolder objects and make use of getItemViewType() as described on this SO question: stackoverflow.com/questions/26245139/… \$\endgroup\$ – AdamMc331 Oct 2 '15 at 17:56
  • \$\begingroup\$ what if I want to use same adapter for different fragments? \$\endgroup\$ – Leena Apr 12 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.