gpt4 book ai didi

android - 如何在 RecyclerView 项目出现时为其设置动画

转载 作者:IT老高 更新时间:2023-10-28 12:52:43 24 4
gpt4 key购买 nike

当 RecyclerView Items 出现时,我如何对其进行动画处理?

默认项目动画器仅在设置回收器数据后添加或删除数据时才动画。我是新开发的应用程序,不知道从哪里开始。

任何想法如何实现这一目标?

最佳答案

编辑:

根据the ItemAnimator documentation :

This class defines the animations that take place on items as changes are made to the adapter.

因此,除非您将项目一一添加到 RecyclerView 并在每次迭代时刷新 View ,否则我认为 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 的更多信息,请参阅 training on the official documentation .

快速滚动问题

使用此方法可能会导致快速滚动出现问题。在动画发生时可以重用 View 。为了避免这种情况,建议在分离时清除动画。

    @Override
public void onViewDetachedFromWindow(final RecyclerView.ViewHolder holder)
{
((CustomViewHolder)holder).clearAnimation();
}

在 CustomViewHolder 上:

    public void clearAnimation()
{
mRootLayout.clearAnimation();
}

旧答案:

看看Gabriele Mariotti's repo , 我很确定你会找到你需要的。他为 RecyclerView 提供了简单的 ItemAnimator,例如 SlideInItemAnimator 或 SlideScaleItemAnimator。

关于android - 如何在 RecyclerView 项目出现时为其设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26724964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com