title: Android Custom View move back to original position when refreshing
body: <p>I'm implementing a time picker which looks like this:  </p>
  
  <p><a href="https://i.stack.imgur.com/gk3V0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gk3V0.png" alt="unfinished time picker"></a></p>
  
  <p>That yellow block is <code>MyCustomView</code>. When moving <code>MyCustomView</code>, I should calculate new date and set <code>tvDate</code>.  </p>
  
  <p>partial layout file:</p>
  
  <pre><code>&lt;LinearLayout 
      android:orientation="vertical"&gt;
  
      &lt;TextView android:id="@+id/tv_date"&gt;
  
      &lt;RelativeLayout 
          android:id="@+id/rl"&gt;
  
          &lt;MyCustomView
           android:layout_centerInParent="true"/&gt;
  
          &lt;OtherViews/&gt;
      &lt;/RelativeLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>code:</p>
  
  <pre><code>class MyCustomView extends View{
  
      // move listener
      public interface IMoveCallback{
          void update(int index);
      }
  
      private IMoveCallback listener = null; 
  
      // set listener
      public void setMoveCallback(IMoveCallback callback){
          this.listener = callback;
      }
  
      @Override
      protected void onDraw(Canvas c){
          super.onDraw(c);
          // draw yellow block and four arrows here.
      }
  
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          processDrag(v, event);
          invalidate();
          return false;
      }
  
      private void processDrag(View v, MotionEvent event){
          // calculate new position(left, top, right, bottom)
          v.layout(newLeft, newTop, newRight, newBottom); 
          if(listener != null){
              // calculate index by new position
              listener.update(index);
          }
      }
  }
  
  class MainActivity extends Activity implements MyCustomView.IMoveCallback{
  
      MyCustomView view; // view.setMoveCallback(MainActivity.this)
  
      @Override
      public void update(int index){
          tvDate.setText(String.valueOf(System.currentTimeMillis()))//update tvDate
      }
  }
  </code></pre>
  
  <p>If <code>tvDate.setText()</code> is removed, <code>MyCustomView</code> follows the finger, like this:  </p>
  
  <p><a href="https://i.stack.imgur.com/2yvik.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2yvik.gif" alt="enter image description here"></a></p>
  
  <p>If I update <code>tvDate</code>, <code>MyCustomView</code> moves back to the center of <code>rl</code>: </p>
  
  <p><a href="https://i.stack.imgur.com/MqIU1.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MqIU1.gif" alt="enter image description here"></a></p>
  
  <p>I don't think it an activity-lifecycle issue. Someone mentioned <code>((MarginLayoutParams)rl.getLayoutParams()).topMargin</code> but did not explain why.<br>
  Anybody can help me?</p>