title: Showing custom View under swiped RecyclerView item
body: <p>First of all, I saw this question: <a href="https://stackoverflow.com/questions/30820806/adding-a-colored-background-with-text-icon-under-swiped-row-when-using-androids">Adding a colored background with text/icon under swiped row when using Android&#39;s RecyclerView</a></p>
  
  <p>However, even though the title states "with text/icon", the answer only covers using <code>Canvas</code> object to draw a rectangle.</p>
  
  <p>Now I have drawing a green rectangle implemented; however I want to make it more obvious what is going to happen by adding "done" button, just like on the following screenshot.</p>
  
  <p><a href="https://i.stack.imgur.com/JUT0V.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JUT0V.jpg" alt="enter image description here"></a></p>
  
  <p>I created a custom view:</p>
  
  <pre><code>public class ChoosePlanView extends LinearLayout{
  
      public ChoosePlanView(Context context) {
          super(context);
          init();
      }
  
      public ChoosePlanView(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();
      }
  
      public ChoosePlanView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          init();
      }
  
      private void init(){
          View v = inflate(getContext(), R.layout.choose_plan_view, null);
          addView(v);
      }
  }
  </code></pre>
  
  <p>And the layout is very simple, consisting of green background and the icon:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/choose_green"&gt;
  
      &lt;ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/doneIcon"
          android:layout_gravity="center_horizontal|right"
          android:src="@mipmap/done"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I tried overriding method <code>OnChildDraw</code>:</p>
  
  <pre><code>// ChoosePlanView choosePlanView; &lt;- that was declared and initialized earlier, so I don't have to create a new ChoosePlanView everytime in OnChildDraw();
  
  ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {    
  
  /*
      onMove, onSwiped omitted
  */
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
         View itemView = viewHolder.itemView;
  
         if(dX &lt; 0){
              choosePlanView.invalidate();
              //choosePlanView.setBackgroundResource(R.color.delete_red);
              choosePlanView.measure(itemView.getWidth(), itemView.getHeight());
              choosePlanView.layout(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
              c.save();
              c.translate(choosePlanView.getRight() + (int) dX, viewHolder.getAdapterPosition()*itemView.getHeight());
  
              choosePlanView.draw(c);
              c.restore();
         }
  
         super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
  }
  </code></pre>
  
  <p>And it kinda works (draws my <code>ChoosePlanView</code>), but there are two problems.</p>
  
  <p>First of all it draws only a small green square wrapping my "done" icon (the red part of the view seen on the screenshot below comes from commented line <code>choosePlanView.setBackgroundResource(R.color.delete_red);</code>). The view has the correct width though, when checked in debugger.</p>
  
  <p>Second thing is where the icon is placed. I specified that I want it to be centered horizontally and always "sticked" to the right side of the view. However it moves, when the <code>RecyclerView</code> item is is swiped, and having red background shows it isn't placed in the center either.</p>
  
  <p>I tried adding line <code>choosePlanView.invalidate()</code>, because I thought that happens, because the view was created earlier, but it seems redrawing changes nothing.</p>
  
  <p><a href="https://i.stack.imgur.com/avo3p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/avo3p.png" alt="enter image description here"></a></p>