Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to Create RecyclerView with Multiple ViewType in Android?

Creating a RecyclerView with multiple view types allows you to display different kinds of items within the same list. The RecyclerView.Adapter class provides the mechanism for this by allowing you to override getItemViewType(int position).

Here's how to create a RecyclerView with multiple view types:

  1. Define Layouts for Each View Type:

    Before you start with the adapter, create XML layout files for each view type. For this example, let's assume you have two types: type_one_layout.xml and type_two_layout.xml.

  2. Modify the Adapter:

    Here's a basic structure for a RecyclerView.Adapter with multiple view types:

    public class MultiViewTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
        private static final int TYPE_ONE = 1;
        private static final int TYPE_TWO = 2;
    
        // Your data source, possibly a List or Array
        private List<Object> items;
    
        public MultiViewTypeAdapter(List<Object> items) {
            this.items = items;
        }
    
        @Override
        public int getItemViewType(int position) {
            // Logic to determine the view type for the given position
            // For this example, let's assume items at even positions will have TYPE_ONE
            // and odd positions will have TYPE_TWO
            return position % 2 == 0 ? TYPE_ONE : TYPE_TWO;
        }
    
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            if (viewType == TYPE_ONE) {
                return new TypeOneViewHolder(inflater.inflate(R.layout.type_one_layout, parent, false));
            } else {
                return new TypeTwoViewHolder(inflater.inflate(R.layout.type_two_layout, parent, false));
            }
        }
    
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (getItemViewType(position) == TYPE_ONE) {
                // Bind data for TYPE_ONE
            } else {
                // Bind data for TYPE_TWO
            }
        }
    
        @Override
        public int getItemCount() {
            return items.size();
        }
    
        public static class TypeOneViewHolder extends RecyclerView.ViewHolder {
            // Your views for TypeOne here
    
            public TypeOneViewHolder(@NonNull View itemView) {
                super(itemView);
                // Initialize your views
            }
        }
    
        public static class TypeTwoViewHolder extends RecyclerView.ViewHolder {
            // Your views for TypeTwo here
    
            public TypeTwoViewHolder(@NonNull View itemView) {
                super(itemView);
                // Initialize your views
            }
        }
    }
    
  3. Initialize RecyclerView:

    With your adapter ready, you can set it up with a RecyclerView:

    RecyclerView recyclerView = findViewById(R.id.your_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    
    List<Object> items = new ArrayList<>();
    // Populate your items
    
    MultiViewTypeAdapter adapter = new MultiViewTypeAdapter(items);
    recyclerView.setAdapter(adapter);
    
  4. Data Management:

    Make sure your data source (items in the above example) supports multiple view types. You might have a single list with mixed object types or a mechanism that helps you identify which view type an item should use.

This is a basic example to get you started. Depending on your needs, you might want to refine the approach, add error handling, introduce more view types, or optimize performance.

  1. Create RecyclerView with different item types in Android:

    • In your RecyclerView adapter, override the getItemViewType method to return different view types based on your data.
    @Override
    public int getItemViewType(int position) {
        // Return different view types based on position or data
        if (yourDataSet.get(position) instanceof TypeA) {
            return VIEW_TYPE_A;
        } else if (yourDataSet.get(position) instanceof TypeB) {
            return VIEW_TYPE_B;
        }
        // Add more conditions for additional view types
    }
    
  2. Android RecyclerView with heterogeneous layouts:

    • Modify your onCreateViewHolder method to inflate different layouts based on the view type.
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        switch (viewType) {
            case VIEW_TYPE_A:
                view = inflater.inflate(R.layout.item_type_a, parent, false);
                return new TypeAViewHolder(view);
            case VIEW_TYPE_B:
                view = inflater.inflate(R.layout.item_type_b, parent, false);
                return new TypeBViewHolder(view);
            // Add more cases for additional view types
        }
    }
    
  3. Implementing dynamic view types in RecyclerView Android:

    • Dynamically set different view types based on your data.
    @Override
    public int getItemViewType(int position) {
        if (yourDynamicCondition) {
            return VIEW_TYPE_A;
        } else {
            return VIEW_TYPE_B;
        }
    }
    
  4. Handling different view types in RecyclerView Android:

    • Create different ViewHolder classes for each view type and handle them in the onBindViewHolder method.
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case VIEW_TYPE_A:
                ((TypeAViewHolder) holder).bind((TypeA) yourDataSet.get(position));
                break;
            case VIEW_TYPE_B:
                ((TypeBViewHolder) holder).bind((TypeB) yourDataSet.get(position));
                break;
            // Add more cases for additional view types
        }
    }
    
  5. Code example for RecyclerView with multiple view types in Android:

    • Here is an example of a simple implementation:
    public class YourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private static final int VIEW_TYPE_A = 1;
        private static final int VIEW_TYPE_B = 2;
    
        // ... other necessary code
    
        @Override
        public int getItemViewType(int position) {
            if (yourDataSet.get(position) instanceof TypeA) {
                return VIEW_TYPE_A;
            } else if (yourDataSet.get(position) instanceof TypeB) {
                return VIEW_TYPE_B;
            }
            return super.getItemViewType(position);
        }
    
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            View view;
            switch (viewType) {
                case VIEW_TYPE_A:
                    view = inflater.inflate(R.layout.item_type_a, parent, false);
                    return new TypeAViewHolder(view);
                case VIEW_TYPE_B:
                    view = inflater.inflate(R.layout.item_type_b, parent, false);
                    return new TypeBViewHolder(view);
                default:
                    throw new IllegalArgumentException("Invalid view type");
            }
        }
    
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            switch (holder.getItemViewType()) {
                case VIEW_TYPE_A:
                    ((TypeAViewHolder) holder).bind((TypeA) yourDataSet.get(position));
                    break;
                case VIEW_TYPE_B:
                    ((TypeBViewHolder) holder).bind((TypeB) yourDataSet.get(position));
                    break;
                default:
                    throw new IllegalArgumentException("Invalid view type");
            }
        }
    
        // ViewHolder classes and other methods
    }