RecyclerView

Multiple row layouts using RecyclerView

Alex Zhukovich 4 min read
Multiple row layouts using RecyclerView
Table of Contents

Today I would like to talk about using different row layouts using RecyclerView.

Multiple row layouts using RecyclerView screen

Firstly we need to add some dependencies to build.gradle file for using CardView and RecyclerView. For it need to update dependency section in this file:

compile 'com.android.support:cardview-v7:23.2.0'
compile 'com.android.support:recyclerview-v7:23.2.0'

Right now we can start working with RecyclerView. In this short tutorial I'll create list with Cities and Events which contains different object in the same list is not the best idea, but in purposes for tutorials it should be ok.

If you want to use different types of row layouts you must to implement next method in adapter:

@Override
public int getItemViewType(int position) {
    ...
}

Firstly we can create two different layouts for our RecyclerView.

item_city:

<?xml version="1.0" encoding="utf-8"?><br />
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:textSize="28sp" />
</android.support.v7.widget.CardView>

item_event:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="32dp"
    android:paddingBottom="12dp">
    
    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:textSize="24sp"/>
   <TextView
        android:id="@+id/descriptionTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:textSize="18sp" />
</LinearLayout>

Next step is creating a model and dummy data for the application. Firstly need to create the model class which called CityEvent.

public class CityEvent {
    public static final int CITY_TYPE = 0;
    public static final int EVENT_TYPE = 1;

    private String mName;
    private String mDescription;
    private int mType;

    public CityEvent(String name, String description, int type) {
        this.mName = name;
        this.mDescription = description;
        this.mType = type;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        this.mName = name;
    }

    public String getDescription() {
        return mDescription;
    }

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

    public int getType() {
        return mType;
    }

    public void setType(int type) {
        this.mType = type;
    }
}

After it, I created DummyData class.

public final class DummyData {

    public static List getData() {
        List list = new ArrayList<>();
        list.add(new CityEvent(“London”, null, CityEvent.CITY_TYPE));
        list.add(new CityEvent(“Droidcon”, “Droidcon in London”, CityEvent.EVENT_TYPE));
        list.add(new CityEvent(“Some event”, “Some event in London”, CityEvent.EVENT_TYPE));
        list.add(new CityEvent(“Amsterdam”, null, CityEvent.CITY_TYPE));
        list.add(new CityEvent(“Droidcon”, “Droidcon in Amsterdam”, CityEvent.EVENT_TYPE));
        list.add(new CityEvent(“Berlin”, null, CityEvent.CITY_TYPE));
        list.add(new CityEvent(“Droidcon”, “Droidcon in Berlin”, CityEvent.EVENT_TYPE));
        return list;
    }
}

After it, we can implement adapter for RecyclerView. This class called DifferentRowAdapter.

public class DifferentRowAdapter extends RecyclerView.Adapter {
    private List mList;

    public DifferentRowAdapter(List list) {
        this.mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;

        switch (viewType) {
            case CITY_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city, parent, false);
                return new CityViewHolder(view);
            case EVENT_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_event, parent, false);
                return new EventViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CityEvent object = mList.get(position);
        if (object != null) {
            switch (object.getType()) {
                case CITY_TYPE:
                    ((CityViewHolder) holder).mTitle.setText(object.getName());
                    break;
                case EVENT_TYPE:
                    ((EventViewHolder) holder).mTitle.setText(object.getName());
                    ((EventViewHolder) holder).mDescription.setText(object.getDescription());
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            CityEvent object = mList.get(position);
            if (object != null) {
                return object.getType();
            }
        }
        return 0;
    }

    public static class CityViewHolder extends RecyclerView.ViewHolder {
        private TextView mTitle;

        public CityViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.titleTextView);
        }
    }

    public static class EventViewHolder extends RecyclerView.ViewHolder {
        private TextView mTitle;
        private TextView mDescription;

        public EventViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.titleTextView);
            mDescription = (TextView) itemView.findViewById(R.id.descriptionTextView);
        }
    }
}

The last step is updating the MainActivity class.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();

        DifferentRowAdapter adapter = new DifferentRowAdapter(DummyData.getData());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(adapter);
    }
}

Full source code you can find on GitHub.


Mobile development with Alex

A blog about Android development & testing, Best Practices, Tips and Tricks

Share
More from Mobile development with Alex

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile development with Alex.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.