java 如何为新项目添加 RecyclerView 滑入动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37444739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Add RecyclerView Slide In Animation for New Item
提问by tccpg288
I have a RecyclerView and add items to mCommentArrayList
at index 0. I am trying to create a slide-in animation at the top of the view as new items (CardViews) are added to the RecyclerView.
我有一个 RecyclerView 并将项目添加到mCommentArrayList
索引 0。当新项目 (CardViews) 添加到 RecyclerView 时,我试图在视图顶部创建一个滑入动画。
I know there are libraries that can be used, and I have even explored https://github.com/wasabeef/recyclerview-animators. However, the documentation is limited and I am unsure what approach to take.
我知道有可以使用的库,我什至探索过https://github.com/wasabeef/recyclerview-animators。但是,文档有限,我不确定要采取什么方法。
Note that I add all new items to my mCommentArrayList
at index 0
so that they appear at the top of the view. I know there is some work to be done in the adapter, specifically onBindViewHolder()
, but I don't know exactly what to put in order to activate the animations.
请注意,我将所有新项目添加到我的mCommentArrayList
at 中,index 0
以便它们出现在视图的顶部。我知道在适配器中有一些工作要做,特别是onBindViewHolder()
,但我不知道确切地放什么来激活动画。
Where I first call to Firebase to find data to populate the RecyclerView:
我首先调用 Firebase 查找数据以填充 RecyclerView:
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) {
}
});
Subsequent Calls to Firebase on Data Change:
在数据更改时对 Firebase 的后续调用:
@Override
protected void onStart() {
super.onStart();
mUpdateComments = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mNumberOfCommentsAtPoll = (int) dataSnapshot.getChildrenCount();
for (DataSnapshot x : dataSnapshot.child(COMMENTS_LABEL).getChildren()) {
Log.v("DATA_SNAPSHOT", x.toString());
if (mCommentIDArrayList.contains(x.getKey())) {
Log.v("Comment_Already_Added", x.getKey());
} else {
Log.v("Child_Added_Called", "Child Added Called");
mCommentIDArrayList.add(x.getKey());
String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(x.getKey()).child("COMMENT").getValue();
Log.v("New_Comment", "The new comment is " + commentID);
String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(x.getKey()).child("USER_ID").getValue();
Log.v("New_User_ID", "The new userID is " + userID);
mCommentArrayList.add(0, new Comments(mUserAvatar, userID, commentID));
mPollCommentsList.getAdapter().notifyItemInserted(0);
}
}
}
回答by Jeffalee
Using the library you were talking about (https://github.com/wasabeef/recyclerview-animators) it's very easy to add a SlideInAnimator
to your RecyclerView
. Just use the following code to set an Animator
to your RecyclerView
(pick one):
使用您正在谈论的库(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());
Once you have done this the you can simply trigger the animation by calling notifyItemInserted(position)
or notifyItemRangeInserted(positionStart, itemCount)
. These calls will trigger the Animator
, calling notifyDatasetChanged()
won't.
完成此操作后,您只需调用notifyItemInserted(position)
或即可触发动画notifyItemRangeInserted(positionStart, itemCount)
。这些调用将触发Animator
,调用notifyDatasetChanged()
不会。
Triggering the insertion animation:
触发插入动画:
recyclerView.getAdapter().notifyItemInserted(position);
recyclerView.getAdapter().notifyItemRangeInserted(positionStart, itemCount);
回答by Saurabh Gaddelpalliwar
Hope this code help's you !
希望这段代码对你有帮助!
create one animation file animation_from_right.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>
in Activity
在活动中
Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.animation_from_right);
holder.itemView.startAnimation(animation);
use above code in your Adapter on onBindViewHolder
在 onBindViewHolder 上的适配器中使用上面的代码
回答by Vaibhav Sharma
The best way animate the recyclerview will be to do it in the onbindviewholder method
.
为 recyclerview 设置动画的最佳方法是在onbindviewholder method
.
Here is how to do it-
这是如何做到的 -
create a field variable lastAnimatedPosition in the adapter class.
在适配器类中创建一个字段变量 lastAnimatedPosition。
private int lastAnimatedPosition = -1;
Then in the onbindviewholder-
然后在 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();
}
}
Next a few tweaks in your viewholder class-
接下来在你的 viewholder 类中进行一些调整 -
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();
}**
}
And lastly simply override onViewDetachedFromWindow
-
最后简单地覆盖onViewDetachedFromWindow
-
@Override
public void onViewDetachedFromWindow(final ViewHolder holder) {
holder.clearAnimation();
}
UPDATE
更新
Since the element to be animated is in the 0th index replace the if (position > lastAnimatedPosition)
snippet with -
由于要设置动画的元素位于第 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();
}