title: Custom view with canvas draw in Recyclerview not working
body: <p>I'm trying to draw an arc from one view to another (canvas drawing) inside recyclerview. So I added a custom view in recyclerview layout. But it's not getting drawn properly. It draws randomly for some items in recyclerview. Here's my code :</p>
  
  <p><strong>ArcView</strong></p>
  
  <pre><code>public class ArcView extends View {
  
      final RectF oval = new RectF();
      Path myPath = new Path();
      Paint paint;
      float left, top, right, bottom;
  
      /**/
      private float mCanvasCenterX;
  
      private float mCenterCircleWidth, mCenterCircleHeight;
  
      /**/
      public ArcView(Context context) {
          super(context);
      }
      public ArcView(Context context, AttributeSet attrs) {
          super(context, attrs);
          init(attrs);
      }
      public ArcView(Context context, AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
          init(attrs);
      }
  
      private void init(AttributeSet set) {
          if (set == null)
              return;
          paint = new Paint(Paint.ANTI_ALIAS_FLAG);
          setLayerType(View.LAYER_TYPE_SOFTWARE, null);
          /*TypedArray typedArray = getContext().obtainStyledAttributes(set, R.styleable.FlightStatusArcView);
          typedArray.recycle();*/
      }
  
      public void setArcProperties(float l, float t, float r, float b) {
          left = l;
          top = t;
          right = r;
          bottom = b;
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
          oval.set(left, top, right, bottom);
          paint.setColor(Color.WHITE);
          paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeWidth(5f);
  
          canvas.drawArc(oval, 180f, 180f, false, paint);
          drawArc(canvas);
  
  
  
  
      }
  
  
  
      private void drawArc(Canvas canvas) {
  
          canvas.save();
  
          Paint p = new Paint();
          p.setColor(Color.WHITE);
          p.setStyle(Paint.Style.FILL);
          p.setStrokeWidth(4f);
  
          float startX = oval.left + 20;
          float startY = oval.top + (oval.centerY() - oval.top)/ 2;
  
          Path path = new Path();
          path.moveTo(startX, startY);
          path.lineTo(startX + 20, startY + 20);
          path.lineTo(startX + 20, startY);
          path.lineTo(startX + 20, startY);
          path.close();
  
          canvas.drawPath(path, p);
  
          canvas.restore();
      }
  
  
  }
  </code></pre>
  
  <p><strong>activity_main</strong>, layout of MainActivity which holds the recyclerview</p>
  
  <pre><code>&lt;androidx.constraintlayout.widget.ConstraintLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity"&gt;
  
      &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello World!"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent"/&gt;
  
  
      &lt;androidx.recyclerview.widget.RecyclerView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/recycler_view"
              android:paddingEnd="5dp"
              android:paddingStart="5dp"
              android:scrollbars="vertical"&gt;
  
      &lt;/androidx.recyclerview.widget.RecyclerView&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p><strong>MyAdapter</strong> , adapter class for recyclerview. Here, I'm trying to draw the canvas.But it doesn't draw anything.</p>
  
  <pre><code>class MyAdapter(var items: ArrayList&lt;Item&gt;) :
      RecyclerView.Adapter&lt;MyAdapter.MyViewHolder&gt;() {
      fun updateItems(newItems: List&lt;Item&gt;) {
          items.clear()
          items.addAll(newItems)
          notifyDataSetChanged()
      }
      override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = MyViewHolder(
          LayoutInflater.from(parent.context).inflate(R.layout.activity_arc_view, parent, false)
      )
      override fun getItemCount() = items.size
      override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
          holder.bind(items[position])
          val handler = Handler()
          handler.postDelayed(Runnable {
              setPropertiesAndDrawArc(holder)
          }, 1500)
      }
      class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
          val from = view.from_airport
          val to = view.to_airport
          val customView = view.customView
          val circleStart = view.circle_start
          val circleEnd = view.circle_end
          val inFlight = view.in_flight
          val flyingTime = view.flying_time
          fun bind(item: Item) {
              from.text = item.from
              to.text = item.to
          }
      }
      private fun setPropertiesAndDrawArc(holder: MyViewHolder) {
          val circleStartLocation = IntArray(2)
          holder.circleStart.getLocationOnScreen(circleStartLocation)
          val circleEndLocation = IntArray(2)
          holder.circleEnd.getLocationOnScreen(circleEndLocation)
          val inFlightLocation = IntArray(2)
          holder.inFlight.getLocationOnScreen(inFlightLocation)
          val fromAirportLocation = IntArray(2)
          holder.from.getLocationOnScreen(fromAirportLocation)
          val flyingTimeLocation = IntArray(2)
          holder.flyingTime.getLocationOnScreen(flyingTimeLocation)
          val toAirportLocation = IntArray(2)
          holder.to.getLocationOnScreen(toAirportLocation)
          holder.flyingTime.measure(0, 0)
          val flyingTimeHeight = holder.flyingTime.getMeasuredHeight()
          val flyingTimeTop = flyingTimeLocation[1] - flyingTimeHeight
          val left = circleStartLocation[0] + holder.circleStart.getWidth() / 2
          val top = inFlightLocation[1] + (flyingTimeTop - inFlightLocation[1]) / 2
          val right = circleEndLocation[0] + holder.circleEnd.getWidth() / 2
          val bottom = circleStartLocation[1] + (circleStartLocation[1] - inFlightLocation[1]) / 2
          holder.customView.setArcProperties(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
          holder.customView.invalidate()
      }
  }
  </code></pre>
  
  <p><strong>activity_arc_view</strong>, the recyclerview item row layout</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;com.example.customviewinrv.ArcView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/customView"
          android:elevation="2dp"
          android:layout_marginTop="0dp"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"/&gt;
  
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:id="@+id/container"
          android:layout_width="match_parent"
          android:layout_height="170dp"
          android:background="@color/colorPrimaryDark"
          android:visibility="visible"
          android:layout_marginTop="20dp"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"&gt;
  
          &lt;View
              android:id="@+id/circle_start"
              android:layout_width="12dp"
              android:layout_height="12dp"
              android:background="@drawable/circle_white"
              app:layout_constraintBottom_toTopOf="@id/from_airport"
              app:layout_constraintEnd_toEndOf="@id/from_airport"
              app:layout_constraintStart_toStartOf="@id/from_airport" /&gt;
  
          &lt;View
              android:id="@+id/circle_end"
              android:layout_width="12dp"
              android:layout_height="12dp"
              android:background="@drawable/circle_white"
              app:layout_constraintBottom_toTopOf="@id/to_airport"
              app:layout_constraintEnd_toEndOf="@id/to_airport"
              app:layout_constraintStart_toStartOf="@id/to_airport" /&gt;
  
  
          &lt;TextView
              android:id="@+id/from_airport"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="10dp"
              android:gravity="center"
              android:text="Trivandrum\nInternational Airport"
              android:textColor="#FFFFFF"
              android:textSize="19sp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent" /&gt;
  
          &lt;TextView
              android:id="@+id/to_airport"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="10dp"
              android:gravity="center"
              android:text="Mineralnye Vody \nAirport"
              android:textColor="#FFFFFF"
              android:textSize="19sp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintRight_toRightOf="parent" /&gt;
  
          &lt;TextView
              android:id="@+id/in_flight"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="10dp"
              android:gravity="center"
              android:text="In-flight"
              android:textColor="#FFFFFF"
              android:textSize="19sp"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent" /&gt;
  
          &lt;TextView
              android:id="@+id/flying_time"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="30dp"
              android:gravity="center"
              android:text="Flying Time\n03h 30m"
              android:textColor="#FFFFFF"
              android:textSize="11sp"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toBottomOf="@id/in_flight" /&gt;
  
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>It works well if it's on a normal activity without recyclerview. I wonder how do I achieve it inside recyclerview. Please help.</p>