我有一个用CardView布局填充的回收视图。我注意到,在许多应用程序中,这些项目都有一个漂亮的动画入口,而不是像我的应用程序中那样突然出现。下面是我正在尝试实现的一个示例:
我如何使用我的swipeRefreshListener/适配器来触发这个动画?我如何通过我的适配器实现这一点?
注意:我需要在RecyclerView第一次填充CardView项及其包含的数据时触发此动画。
My 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来设置延迟,但是访问每个子进程都是一个问题。
发布于 2015-09-03 15:21:52
使用这个自定义的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);
}
}
演示
发布于 2015-09-03 12:58:12
首先定义动画:
<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;
}
}
发布于 2015-08-27 21:34:05
对于RecyclerView,您可以通过以下方法设置它们来使用ItemAnimators:
RecyclerView.setItemAnimator(RecyclerView.ItemAnimator animator)
有几个库实现了这个ItemAnimator,例如这个:https://github.com/wasabeef/recyclerview-animators
https://stackoverflow.com/questions/32239881
复制相似问题