问题描述
我如何动画RecyclerViews时有出现?
How can I animate the RecyclerViews when there are appearing?
当数据被添加或删除回收站数据已设置后的默认项动画只动画。我是新开发的应用程序,并且没有任何线索从哪里开始。
The default item animator only animates when a data is added or removed after the recycler data has been set. I'm new developing applications, and don't have any clue where to start.
任何想法如何实现这一目标?
Any ideas how to achieve this?
推荐答案
编辑:
据the ItemAnimator文档 :
本类定义上发生的项目,如更改了适配器的动画。
所以,除非你通过一次加你的项目之一,你的 RecyclerView
键,刷新视图在每次迭代中,我不认为 ItemAnimator
是解决您的需要。
So unless you add your items one by one to your RecyclerView
and refresh the view at each iteration, I don't think ItemAnimator
is the solution to your need.
下面是如何出现时使用CustomAdapter他们能够绘制的 RecyclerView
项目:
Here is how you can animate the RecyclerView
items when they appear using a 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.container, 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是这样的:
And your custom_item_layout would look like this :
<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
,请参阅本的。
For more information about CustomAdapters and RecyclerView
, refer to this training on the official documentation.
旧的回答:
给看看 加布里埃莱·马里奥蒂的回购 ,我是pretty的相信你终有一天会发现你所需要的。他提供简单ItemAnimators为RecyclerView,如SlideInItemAnimator或SlideScaleItemAnimator。
Give a look at Gabriele Mariotti's repo, I'm pretty sure you'll find what you need. He provides simple ItemAnimators for the RecyclerView, such as SlideInItemAnimator or SlideScaleItemAnimator.
这篇关于他们出现时,如何进行动画RecyclerView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!