如何为新项目添加RecyclerView幻灯片动画

时间:2016-05-25 18:13:11

标签: java android xml android-layout android-recyclerview

我有一个RecyclerView并在索引0处向mCommentArrayList添加项目。我正在尝试在视图顶部创建一个滑入式动画,因为新项目(CardViews)已添加到RecyclerView。

我知道有可以使用的库,我甚至已经探索过https://github.com/wasabeef/recyclerview-animators。但是,文档是有限的,我不确定采取什么方法。

请注意,我将所有新项目添加到mCommentArrayList的{​​{1}},以便它们显示在视图的顶部。我知道在适配器中有一些工作要做,特别是index 0,但我不确切知道要激活动画的内容。

我首先致电Firebase查找填充RecyclerView的数据:

onBindViewHolder()

随后调用Firebase进行数据更改:

    mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            setImage(dataSnapshot);
            setQuestion(dataSnapshot);
            createInitialCommentIDArray(dataSnapshot);
            mNumberOfCommentsAtPoll = (int) dataSnapshot.child(COMMENTS_LABEL).getChildrenCount();
            for (int i = 0; i < mNumberOfCommentsAtPoll; i++) {
                String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("COMMENT").getValue();
                Log.v("COMMENT_ID", "The comment ID is " + commentID);
                String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("USER_ID").getValue();
                Log.v("USER_ID", "The user ID is " + userID);
                mCommentArrayList.add(0, new Comments(mUserAvatar, userID, commentID));
                mCommentAdapter.notifyDataSetChanged();
            }

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

3 个答案:

答案 0 :(得分:3)

使用您正在讨论的库(https://github.com/wasabeef/recyclerview-animators),可以非常轻松地向SlideInAnimator添加RecyclerView。只需使用以下代码将Animator设置为RecyclerView(选择一个):

    recyclerView.setItemAnimator(new SlideInDownAnimator());
    recyclerView.setItemAnimator(new SlideInRightAnimator());
    recyclerView.setItemAnimator(new SlideInLeftAnimator());
    recyclerView.setItemAnimator(new SlideInUpAnimator());

完成此操作后,您只需拨打notifyItemInserted(position)notifyItemRangeInserted(positionStart, itemCount)即可触发动画。这些来电会触发Animator,呼叫notifyDatasetChanged() 赢得

触发插入动画:

    recyclerView.getAdapter().notifyItemInserted(position);
    recyclerView.getAdapter().notifyItemRangeInserted(positionStart, itemCount);

答案 1 :(得分:1)

为Recyclerview设置动画的最佳方式是在onbindviewholder method

中执行此操作

以下是如何操作 -

在适配器类中创建一个字段变量lastAnimatedPosition。

private int lastAnimatedPosition = -1;

然后在onbindviewholder -

 @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Comments comment = mDataSet.get(position);
        holder.userComment.setText(comment.getUserComment());
        holder.userID.setText("User " + position);
if (position > lastAnimatedPosition) {
      lastAnimatedPosition = position;
      Animation animation = AnimationUtils.loadAnimation(context, R.anim.my_anim_set);
      animation.setInterpolator(new AccelerateDecelerateInterpolator());
      ((ViewHolder) holder).container.setAnimation(animation);
      animation.start();
    }
    }

接下来在你的观察者类中进行一些调整 -

public class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        protected ImageView userAvatar;
        protected TextView userID;
        protected TextView userComment;
        **protected View container;**


        public ViewHolder(View v) {
            super(v);
            **container = v;**
            userAvatar = (ImageView) v.findViewById(R.id.profile_image);
            userID = (TextView) v.findViewById(R.id.user_ID);
            userComment = (TextView) v.findViewById(R.id.user_comment_textview);
        }

        **public void clearAnimation() {
          container.clearAnimation();
        }**
    }

最后简单地覆盖onViewDetachedFromWindow -

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

<强>更新

由于要动画的元素位于第0个索引中,因此使用 -

替换if (position > lastAnimatedPosition)代码段
if (position == 0) {
      lastAnimatedPosition = position;
      Animation animation = AnimationUtils.loadAnimation(context, R.anim.my_anim_set);
      animation.setInterpolator(new AccelerateDecelerateInterpolator());
      ((ViewHolder) holder).container.setAnimation(animation);
      animation.start();
    }

答案 2 :(得分:0)

希望此代码对您有所帮助!

创建一个动画xml

animation_from_right.xml
 <?xml version="1.0" encoding="utf-8"?>

 <set xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="700"
  android:fillAfter="false"
 >

  <translate
    android:interpolator="@android:anim/decelerate_interpolator"
    android:fromXDelta="100%p"
    android:toXDelta="0"
    />

   <alpha
    android:fromAlpha="0.5"
    android:toAlpha="1"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    />

 </set>

Animation animation = AnimationUtils.loadAnimation(mActivity,R.anim.animation_from_right);         owner.itemView.startAnimation(animation);

在onBindViewHolder的适配器中使用以上代码