我有一个回收的视图,其中填充了CardView布局。我注意到在很多应用程序中,这些项目都有一个很好的动画入口,而不是像我们目前在我的应用程序中那样突然出现。我想要实现的一个例子如下所示:
我如何使用swipeRefreshListener / adapter来触发此动画?我如何通过我的适配器实现这一点?
注意:当第一次使用CardView项目及其包含的数据填充RecyclerView时,我也需要触发此动画。
我的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()
方法来遍历项目,但这并不起作用。不知何故,我需要单独访问/动画每个孩子,并可能在每个动画之间设置延迟。但是,我不确定我究竟能做到这一点。我知道我可以使用Handler来设置延迟,但访问每个孩子都是一个问题。
答案 0 :(得分:43)
使用此自定义类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);
}
}
<强>样本强>
答案 1 :(得分:4)
首先定义动画:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="300" />
</set>
然后在适配器中设置它:
@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.push_left_in);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
答案 2 :(得分:3)
对于RecyclerView,您可以使用此方法设置ItemAnimators:
RecyclerView.setItemAnimator(RecyclerView.ItemAnimator animator)
有一些库可以实现这个ItemAnimator,例如这个:https://github.com/wasabeef/recyclerview-animators