title: Setting view's margin many times in an efficient manner
body: <p>I am developing an android app which contains a fragment. This fragment looks like youtube's video play fragment. Everything was going well but now I've encountered a problem concerning the memory usage. </p>
  
  <p>I need to change some margin for animation. For doing that, I use a scroll listener to get coordinates between 0 and 1. This is how I set margin of my layout:</p>
  
  <pre><code>void setMargin(View view1, int top) {
      ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view1.getLayoutParams();
      params.topMargin = top;
      view1.requestLayout();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Override
  public void onSlide(@NonNull View bottomSheet, float slideOffset) {
      if (slideOffset &gt;= 0) {
          int ii = (int) (height * (1 - slideOffset) + (playerViewFirstHeight * slideOffset));
          setMargin(frame_middle, ii);
      } else { }
  }
  </code></pre>
  
  <p>My view is that.</p>
  
  <pre><code>&lt;FrameLayout
      android:id="@+id/frame_middle"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginTop="200dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/frame_header"&gt;
  
      &lt;ProgressBar
          android:id="@+id/progressB_frame_middle_fr_playing"
          style="?android:attr/progressBarStyle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center" /&gt;
  
      &lt;android.support.v4.widget.NestedScrollView
          android:id="@+id/scrollV_frame_middle_fr_playing"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/frame_header"&gt;
  
          &lt;LinearLayout
              android:id="@+id/linearL_frame_middle_fr_playing"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:focusableInTouchMode="true"
              android:orientation="vertical"&gt;
  
              &lt;android.support.constraint.ConstraintLayout
                  android:id="@+id/constraintL_video_information"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:padding="8dp"&gt;
  
                  &lt;LinearLayout
                      android:id="@+id/linearLayout"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="8dp"
                      android:orientation="horizontal"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintTop_toBottomOf="@+id/textView6"&gt;
  
                      &lt;ImageView
                          android:id="@+id/imageView"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_weight="1"
                          android:tint="#757575"
                          app:layout_constraintStart_toStartOf="parent"
                          app:layout_constraintTop_toBottomOf="@+id/textV_published_at"
                          app:srcCompat="@drawable/ic_thumb_up_black_24dp" /&gt;
  
                      &lt;TextView
                          android:id="@+id/textView4"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginStart="4dp"
                          android:layout_weight="1"
                          app:layout_constraintBottom_toBottomOf="@+id/imageView"
                          app:layout_constraintStart_toEndOf="@+id/imageView" /&gt;
  
                      &lt;ImageView
                          android:id="@+id/imageView2"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_marginStart="8dp"
                          android:layout_weight="1"
                          android:tint="#757575"
                          app:layout_constraintBottom_toBottomOf="@+id/textView4"
                          app:layout_constraintStart_toEndOf="@+id/textView4"
                          app:srcCompat="@drawable/ic_thumb_down_black_24dp" /&gt;
  
                      &lt;TextView
                          android:id="@+id/textView5"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginStart="4dp"
                          android:layout_weight="1"
                          app:layout_constraintBottom_toBottomOf="@+id/imageView2"
                          app:layout_constraintStart_toEndOf="@+id/imageView2" /&gt;
                  &lt;/LinearLayout&gt;
  
                  &lt;ImageView
                      android:id="@+id/imageV_whatsapp"
                      android:layout_width="24dp"
                      android:layout_height="24dp"
                      android:layout_marginBottom="8dp"
                      android:layout_marginEnd="8dp"
                      android:tint="#757575"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintEnd_toEndOf="parent"
                      app:srcCompat="@drawable/ic_share_black_24dp" /&gt;
  
                  &lt;TextView
                      android:id="@+id/textV_video_category"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginStart="8dp"
                      android:textColor="@color/colorAccent"
                      app:layout_constraintBottom_toBottomOf="@+id/textView6"
                      app:layout_constraintStart_toEndOf="@+id/textView6"
                      app:layout_constraintTop_toTopOf="@+id/textView6" /&gt;
  
                  &lt;TextView
                      android:id="@+id/textV_video_channel_name"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="8dp"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintTop_toBottomOf="@+id/textV_video_title" /&gt;
  
                  &lt;TextView
                      android:id="@+id/textV_video_title"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_alignParentTop="true"
                      android:gravity="center_vertical"
                      android:textColor="@android:color/black"
                      android:textSize="18sp"
                      app:layout_constraintEnd_toEndOf="parent"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintTop_toTopOf="parent" /&gt;
  
                  &lt;View
                      android:id="@+id/view2"
                      android:layout_width="match_parent"
                      android:layout_height="1dp"
                      android:layout_marginTop="8dp"
                      android:background="#e0e0e0"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintEnd_toEndOf="parent"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintTop_toBottomOf="@+id/linearLayout" /&gt;
  
                  &lt;ImageView
                      android:id="@+id/imageV_download"
                      android:layout_width="24dp"
                      android:layout_height="24dp"
                      android:layout_marginBottom="8dp"
                      android:layout_marginEnd="8dp"
                      android:tint="#757575"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintEnd_toStartOf="@+id/imageV_whatsapp"
                      app:srcCompat="@drawable/ic_download_button" /&gt;
  
                  &lt;TextView
                      android:id="@+id/textView6"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="8dp"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintTop_toBottomOf="@+id/textV_video_channel_name" /&gt;
              &lt;/android.support.constraint.ConstraintLayout&gt;
  
              &lt;RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"&gt;
  
                  &lt;TextView
                      android:id="@+id/textView"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginStart="8dp"
                      android:layout_marginTop="4dp"
                      android:textColor="@android:color/black" /&gt;
  
                  &lt;android.support.v7.widget.RecyclerView
                      android:id="@+id/recyclerV_related_videos"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_below="@+id/textView" /&gt;
              &lt;/RelativeLayout&gt;
  
          &lt;/LinearLayout&gt;
      &lt;/android.support.v4.widget.NestedScrollView&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>To sum up, I run the above method many times when scrolling. Thus, memory usage increases a lot. Despite destroying this fragment, memory usage stays almost same. It seems to me, my view draws itself many times and doesn't clear previous instances of itself.</p>
  
  <p>I couldn't find any solution for that and I really need your ideas.
  Thanks in advance.</p>