添加2个新功能(swipelayout + animation)后,我的ui会冻结。
我有一个包含电影对象的recyclerview。如果我添加/删除/修改电影,我会调用我的电影管理员,因为它有一个通知概念(所有想要使用数据的视图都必须在此管理器上注册)。 在添加新功能之前,一切正常。
我使用此swipelayout: https://github.com/daimajia/AndroidSwipeLayout
目前这些项目如下:
刷完后:
如果我有4个项目,冻结总是可以重现的。我在第二个项目上滑动并删除它。然后它会像我想要的那样消失。 在我刷了" new"第二项是冻结10-15秒。
项目的布局xml:
<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>
适配器的相关代码:
@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;
}
在CDMMovieManager.getInstance().removeMovieID(m_List, m_MovieIDs.get(p_iPosition));
中,电影将被删除,所有注册的视图都会收到通知。在这种情况下,此适配器的片段将调用m_Adapter.notifyDataSetChanged();
动画文件:
<?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>
我尝试了View.clearAnimation()
或Animation.setAnimationListener(null)
之类的内容,但我总是遇到这个问题。
Plase询问您是否需要更多信息。
更新
如果我尝试滑动下一个项目,则只会出现问题。 (例如,删除第2项。之后尝试刷第3项,这是新的第二项 - &gt;冻结我尝试滑动的项目)。只有在刷上任何其他项目时才能删除冻结。
答案 0 :(得分:0)
我解决了我的问题。这个答案对我有帮助:How to animate RecyclerView items when they appear
我将此代码添加到我的适配器:
@Override
public void onViewDetachedFromWindow(IC_ViewHolder p_Holder) {
p_Holder.m_View.clearAnimation();
}