title: Recycler view wrap_content with drag and drop
body: <p>I'm trying to create a to-do list just like "google keep" android app.
  Basically, I need a scrolled layout with 2 lists: one of the checked items, and one of the unchecked ones.
  The unchecked list should have an option to drag &amp; drop the items in order to change their position.
  My only problem is that I can't use width=wrap_content in recyclerview, and the layoutManager implementation that I found make the app crash when I try to drag &amp; drop.</p>
  
  <p>For the drag &amp; drop I use <a href="https://github.com/vinc3m1/DragSortAdapter" rel="nofollow">this library.</a></p>
  
  <p>My layout:</p>
  
  <pre><code>&lt;ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fillViewport="true"
  tools:context="views.fragments.ItemsFragment"&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"&gt;
  
          &lt;AutoCompleteTextView
              android:id="@+id/fragment_items_atv_name"
              android:layout_width="0dp"
              android:layout_weight="1"
              android:layout_height="wrap_content" /&gt;
  
          &lt;Button
              android:id="@+id/fragment_items_btn_add"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="8dp"
              android:text="@string/add" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"&gt;
  
          &lt;android.support.v7.widget.RecyclerView
              android:id="@+id/fragment_items_lv_unchecked_list"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
  
          &lt;android.support.v7.widget.RecyclerView
              android:id="@+id/fragment_items_lv_checked_list"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
  
          &lt;TextView
              android:id="@+id/fragment_items_tv_empty"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:text="Empty list"
              android:visibility="gone" /&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>The layout manager I use</p>
  
  <pre><code>public class MyLinearLayoutManager extends LinearLayoutManager {
  
  public MyLinearLayoutManager(Context context)    {
      super(context, VERTICAL, false);
  }
  
  private int[] mMeasuredDimension = new int[2];
  
  @Override
  public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                        int widthSpec, int heightSpec) {
      final int widthMode = View.MeasureSpec.getMode(widthSpec);
      final int heightMode = View.MeasureSpec.getMode(heightSpec);
      final int widthSize = View.MeasureSpec.getSize(widthSpec);
      final int heightSize = View.MeasureSpec.getSize(heightSpec);
      int width = 0;
      int height = 0;
      for (int i = 0; i &lt; getItemCount(); i++) {
          measureScrapChild(recycler, i,
                  View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                  View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                  mMeasuredDimension);
  
          if (getOrientation() == HORIZONTAL) {
              width = width + mMeasuredDimension[0];
              if (i == 0) {
                  height = mMeasuredDimension[1];
              }
          } else {
              height = height + mMeasuredDimension[1];
              if (i == 0) {
                  width = mMeasuredDimension[0];
              }
          }
      }
      switch (widthMode) {
          case View.MeasureSpec.EXACTLY:
              width = widthSize;
          case View.MeasureSpec.AT_MOST:
          case View.MeasureSpec.UNSPECIFIED:
      }
  
      switch (heightMode) {
          case View.MeasureSpec.EXACTLY:
              height = heightSize;
          case View.MeasureSpec.AT_MOST:
          case View.MeasureSpec.UNSPECIFIED:
      }
  
      setMeasuredDimension(width, height);
  }
  
  private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                 int heightSpec, int[] measuredDimension) {
      View view = recycler.getViewForPosition(position);
      if (view != null) {
          RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
          int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                  getPaddingLeft() + getPaddingRight(), p.width);
          int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                  getPaddingTop() + getPaddingBottom(), p.height);
          view.measure(childWidthSpec, childHeightSpec);
          measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
          measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
          recycler.recycleView(view);
      }
  }
  }
  </code></pre>