gpt4 book ai didi

android - RecyclerView 中的 CardView : animation while populating

转载 作者:IT老高 更新时间:2023-10-28 23:03:16 25 4
gpt4 key购买 nike

我有一个循环 View ,其中填充了 CardView 布局。我注意到在许多应用程序中,这些项目有一个很好的动画入口,而不是像它们目前在我的应用程序中那样突然出现。我正在尝试实现的一个示例如下所示: enter image description here

如何使用我的 swipeRefreshListener/adapter 来触发这个动画?我将如何通过我的适配器实现这一点?

Note: I need this animation to also be triggered when the RecyclerView is first populated with the CardView items, and the data they contain.

我的 RecyclerView 的适配器如下图:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

private String[] mDataset;
private String[] mClassDataset;
private int lastPosition = -1;
private Context context;

View view;


public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView mTextView;
public TextView mClassDataView;
CardView container;


public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.grade);
mClassDataView = (TextView)itemView.findViewById(R.id.class_name);
container = (CardView)itemView.findViewById(R.id.card_view);
}
}

public RecyclerAdapter(String[] myDataset, String[] classData, Context context) {
mDataset = myDataset;
mClassDataset = classData;
this.context = context;

}

@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);

ViewHolder vh = new ViewHolder(v);

view = v;

return vh;
}

@Override
public int getItemCount() {
return mClassDataset.length;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {


if (mDataset.length < mClassDataset.length) {

holder.mTextView.setText("N/A");
holder.mClassDataView.setText(mClassDataset[position]);

if (mClassDataset[position].equals("N/A")) {

}

} else {
holder.mTextView.setText(mDataset[position]);
holder.mClassDataView.setText(mClassDataset[position]);
}

for (int i = 0; i < getItemCount(); i++) {

animate(view, i);

}
}

private void animate(final View view, final int position){

Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
view.startAnimation(animation);
lastPosition = position;

}

}

动画目前正在运行,但问题是项目正在同时被动画化。我尝试通过创建自定义 getChild() 方法来遍历项目,但这不起作用。不知何故,我需要单独访问/动画每个 child ,并可能在每个动画之间设置延迟。但是,我不确定我如何能做到这一点。我知道我可以使用 Handler 来设置延迟,但是访问每个 child 是个问题。

最佳答案

使用 RecyclerView 的这个自定义类,它的行为类似于您发布其屏幕截图的应用程序(Reddit Relay),它可以防止在为第一个项目设置动画时滚动。您可能想根据需要自定义动画。

package net.leolink.android.testrecyclerviewentranceanimation;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
* @author Leo on 2015/09/03
*/
public class MyRecyclerView extends RecyclerView {
private boolean mScrollable;

public MyRecyclerView(Context context) {
this(context, null);
}

public MyRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScrollable = false;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return !mScrollable || super.dispatchTouchEvent(ev);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
for (int i = 0; i < getChildCount(); i++) {
animate(getChildAt(i), i);

if (i == getChildCount() - 1) {
getHandler().postDelayed(new Runnable() {
@Override
public void run() {
mScrollable = true;
}
}, i * 100);
}
}
}

private void animate(View view, final int pos) {
view.animate().cancel();
view.setTranslationY(100);
view.setAlpha(0);
view.animate().alpha(1.0f).translationY(0).setDuration(300).setStartDelay(pos * 100);
}
}


演示

Demo

关于android - RecyclerView 中的 CardView : animation while populating,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32239881/

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