title: Android RecyclerView and GridLayoutManager layout
body: <p>I'm trying to implement a <code>RecyclerView</code> with a <code>GridLayoutManager</code> in my Android application. The <code>CardView</code> elements within the <code>RecyclerView</code> are displaying vertically as if they were in a <code>LinearLayout</code>. How can I coerce them to fill the grid from left to right, one row at a time?</p>
  
  <p>Here is my <code>RecyclerView</code> layout code:</p>
  
  <pre><code>&lt;android.support.v7.widget.RecyclerView
      android:id="@+id/RECYCLER_VIEW"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="@dimen/item_offset" /&gt;
  </code></pre>
  
  <p>Here is my <code>CardView</code> layout code:</p>
  
  <pre><code>&lt;android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/card_view"
  card_view:cardUseCompatPadding="true"
  android:layout_width="120dp"
  android:layout_height="120dp"&gt;
  
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;TextView
          android:id="@+id/name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="10dp"
          android:layout_alignParentBottom="true"
          android:textAlignment="center"
          android:textSize="18dp" /&gt;
  
  &lt;/RelativeLayout&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>
  
  <p>Here is the code to set up the <code>RecyclerView</code> in the activity's <code>onCreate</code> method (<code>CustomAdapter</code> extends <code>RecyclerView.Adapter</code>):</p>
  
  <pre><code>        // set up adapter
          adapter = new CustomAdapter(getApplicationContext(), ApplicationState.getGridElements());
          view.setAdapter(adapter);
  
          // grid layout manager with 2 columns
          GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
          view.setLayoutManager(layoutManager);
  
          // custom decoration for equal spacing
          ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getApplicationContext(), R.dimen.item_offset);
          view.addItemDecoration(itemDecoration);
  
          view.setHasFixedSize(true);
  </code></pre>