Search code examples
javaandroidanimationswipe

Animation freeze with swipelayout


after adding 2 new features (swipelayout + animation) my ui freezes.

I have a recyclerview which includes movie objects. If i add/remove/modify a movie i call my moviemanager because there's a notify concept (all views which want to use the data have to be registered on this manager). This all works fine before i added the new features.

I use this swipelayout: https://github.com/daimajia/AndroidSwipeLayout

Currently the items looks like this:

enter image description here

After swiping:

enter image description here

The freeze is always reproducible if i have 4 items. I swipe on the second item and delete it. Then it will dissapear like i want. After im swiping the "new" second item there's a freeze for like 10-15 seconds.

The layout xml of the item:

<com.daimajia.swipe.SwipeLayout
    android:id="@+id/item_movie_swipe_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/item_movie_swipe_bottom_wrapper"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#ff0000">

            <TextView
                android:id="@+id/item_movie_bottom_delete_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/ic_delete_white_24dp"
                android:text="@string/delete"
                android:textColor="#fff"
                android:layout_gravity="center"
                android:padding="10dp"/>
        </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <ImageView
            android:id="@+id/item_movie_list_poster"
            style="@style/image_border_style"
            android:layout_width="68dp"
            android:layout_height="100dp"
            android:layout_marginRight="3dp"
            android:background="@android:color/black"/>

        <TextView
            android:id="@+id/item_movie_list_name_year"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="3dp"
            android:layout_toEndOf="@+id/item_movie_list_poster"
            android:layout_toRightOf="@+id/item_movie_list_poster"
            android:gravity="center|left"
            android:padding="3dp" android:text="name"/>

        <TextView
            android:id="@+id/item_movie_list_genre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/item_movie_list_name_year"
            android:layout_marginBottom="3dp"
            android:layout_toEndOf="@+id/item_movie_list_poster"
            android:layout_toRightOf="@+id/item_movie_list_poster"
            android:padding="3dp"
            android:text="Drama - Komödie - Action" android:textColor="@color/item_textyear"
            android:textSize="12dp"/>

        <TextView
            android:id="@+id/item_movie_list_viewed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/item_movie_list_poster"
            android:layout_toRightOf="@+id/item_movie_list_poster"
            android:paddingLeft="3dp" android:text="Gesehen: "
            android:textColor="@color/accept"
            android:textSize="12dp"/>
    </RelativeLayout>
</com.daimajia.swipe.SwipeLayout>

Relevant code of the adapter:

  @Override
  public void onBindViewHolder(final CDMMoviesAdapter.IC_ViewHolder p_Holder, final int p_iPosition) {

    // more unimportant code...

    p_Holder.m_SwipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);

    // Drag From Right
    p_Holder.m_SwipeLayout.addDrag(SwipeLayout.DragEdge.Right, p_Holder.m_SwipeLayout.findViewById(R.id.item_movie_swipe_bottom_wrapper));

    // Handling different events when swiping
    p_Holder.m_SwipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
      @Override
      public void onClose(SwipeLayout layout) {
        //when the SurfaceView totally cover the BottomView.
      }

      @Override
      public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
        //you are swiping.
      }

      @Override
      public void onStartOpen(SwipeLayout layout) {

      }

      @Override
      public void onOpen(SwipeLayout layout) {
        //when the BottomView totally show.
      }

      @Override
      public void onStartClose(SwipeLayout layout) {

      }

      @Override
      public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
        //when user's hand released.
      }
    });

    p_Holder.m_SwipeLayout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View p_View) {
        if((((SwipeLayout) p_View).getOpenStatus() == SwipeLayout.Status.Close)) {
          Intent l_Intent = new Intent();
          Bundle l_Bundle = new Bundle();
          l_Bundle.putLong(CDMMovieDetailsActivity.ARG_MOVIE, l_MovieID);
          l_Intent.putExtras(l_Bundle);
          l_Intent.setClass(l_Context, CDMMovieDetailsActivity.class);
          l_Context.startActivity(l_Intent);
        }
      }
    });

    p_Holder.m_tvDelete.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View p_View) {
        Animation l_DeleteAnimation = AnimationUtils.loadAnimation(l_Context, R.anim.slide_out);
        l_DeleteAnimation.setAnimationListener(new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {
          }

          @Override
          public void onAnimationRepeat(Animation animation) {
          }

          @Override
          public void onAnimationEnd(Animation animation) {
            mItemManger.removeShownLayouts(p_Holder.m_SwipeLayout);
            CDMMovieManager.getInstance().removeMovieID(m_List, m_MovieIDs.get(p_iPosition));
            mItemManger.closeAllItems();
          }
        });

        p_Holder.m_View.startAnimation(l_DeleteAnimation);
      }
    });

    // mItemManger is member in RecyclerSwipeAdapter Class
    mItemManger.bindView(p_Holder.itemView, p_iPosition);

    // more unimportant code...
  }

  /**
   * This class is the viewholder. For internal use only.
   */
  public static class IC_ViewHolder extends RecyclerView.ViewHolder {

    /**
     * Constructor
     *
     * @param p_View the view
     */
    public IC_ViewHolder(View p_View) {
      super(p_View);

      m_View = p_View;
      m_SwipeLayout = (SwipeLayout) p_View.findViewById(R.id.item_movie_swipe_layout);

      m_tvNameYear = (TextView) p_View.findViewById(R.id.item_movie_list_name_year);
      m_ivPoster = (ImageView) p_View.findViewById(R.id.item_movie_list_poster);
      m_tvGenre = (TextView) p_View.findViewById(R.id.item_movie_list_genre);
      m_tvViewed = (TextView) p_View.findViewById(R.id.item_movie_list_viewed);

      m_tvDelete = (TextView) p_View.findViewById(R.id.item_movie_bottom_delete_desc);
    }

    public View m_View;
    public SwipeLayout m_SwipeLayout;

    public TextView m_tvNameYear;
    public ImageView m_ivPoster;
    public TextView m_tvGenre;
    public TextView m_tvViewed;

    public TextView m_tvDelete;
  }

In CDMMovieManager.getInstance().removeMovieID(m_List, m_MovieIDs.get(p_iPosition)); the movie will be removed and all registered views will be notified. In this case the fragment of this adapter will call m_Adapter.notifyDataSetChanged();

The animation file:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="250"
        android:fromXDelta="0%"
        android:toXDelta="-100%"/>
</set>

I tried things like View.clearAnimation() or Animation.setAnimationListener(null) but i always get this problem. Plase ask if you need more informations.

Update:

Problem only occures if i try to swipe the next item. (For example delete 2nd item. After that try to swipe the 3rd item which is the new second item -> freeze on the item i tried to swipe). The freeze can only be removed on swiping on any other item.


Solution

  • I solved my problem. This answer helped me: How to animate RecyclerView items when they appear

    I added this code to my adapter:

    @Override
    public void onViewDetachedFromWindow(IC_ViewHolder p_Holder) {
      p_Holder.m_View.clearAnimation();
    }