RecyclerView アイテムが表示されたときにアニメーション化するにはどうすればよいですか?
デフォルトのアイテム アニメーターは、リサイクラー データが設定された後にデータが追加または削除された場合にのみアニメーション化されます。
これを実現するにはどうすればよいでしょうか?
ベストアンサー1
編集 :
によるとItemAnimator ドキュメント:
このクラスは、アダプターに変更が加えられたときにアイテムで実行されるアニメーションを定義します。
したがって、アイテムを 1 つずつ追加し、各反復でビューを更新しない限り、ニーズを満たす解決策にはならRecyclerView
ないと思います。ItemAnimator
RecyclerView
CustomAdapter を使用してアイテムが表示されるときにアニメーション化する方法は次のとおりです。
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder>
{
private Context context;
// The items to display in your RecyclerView
private ArrayList<String> items;
// Allows to remember the last item shown on screen
private int lastPosition = -1;
public static class ViewHolder extends RecyclerView.ViewHolder
{
TextView text;
// You need to retrieve the container (ie the root ViewGroup from your custom_item_layout)
// It's the view that will be animated
FrameLayout container;
public ViewHolder(View itemView)
{
super(itemView);
container = (FrameLayout) itemView.findViewById(R.id.item_layout_container);
text = (TextView) itemView.findViewById(R.id.item_layout_text);
}
}
public CustomAdapter(ArrayList<String> items, Context context)
{
this.items = items;
this.context = context;
}
@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_item_layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
holder.text.setText(items.get(position));
// Here you apply the animation when the view is bound
setAnimation(holder.itemView, position);
}
/**
* Here is the key method to apply the animation
*/
private void setAnimation(View viewToAnimate, int position)
{
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition)
{
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
}
custom_item_layout は次のようになります。
<FrameLayout
android:id="@+id/item_layout_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_layout_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
</FrameLayout>
CustomAdaptersとの詳細についてはRecyclerView
、こちらを参照してください。公式文書のトレーニング。
高速スクロールの問題
この方法を使用すると、高速スクロールで問題が発生する可能性があります。アニメーションの実行中にビューが再利用される可能性があります。これを回避するには、デタッチ時にアニメーションをクリアすることをお勧めします。
@Override
public void onViewDetachedFromWindow(final RecyclerView.ViewHolder holder)
{
((CustomViewHolder)holder).clearAnimation();
}
CustomViewHolderの場合:
public void clearAnimation()
{
mRootLayout.clearAnimation();
}
古い回答:
見てみましょうガブリエーレ・マリオッティのレポ、きっと必要なものが見つかるはずです。彼は、SlideInItemAnimator や SlideScaleItemAnimator など、RecyclerView 用のシンプルな ItemAnimator を提供しています。