tcc*_*288 4 java xml android android-layout android-recyclerview
我有一个 RecyclerView 并将项目添加到mCommentArrayList索引 0。当新项目 (CardViews) 添加到 RecyclerView 时,我试图在视图顶部创建一个滑入动画。
我知道有可以使用的库,我什至探索过https://github.com/wasabeef/recyclerview-animators。但是,文档有限,我不确定要采取什么方法。
请注意,我将所有新项目添加到我的mCommentArrayListat 中,index 0以便它们出现在视图的顶部。我知道在适配器中有一些工作要做,特别是onBindViewHolder(),但我不知道确切地放什么来激活动画。
我首先调用 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) {
}
});
Run Code Online (Sandbox Code Playgroud)
在数据更改时对 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用您正在谈论的库(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());
Run Code Online (Sandbox Code Playgroud)
完成此操作后,您只需调用notifyItemInserted(position)或即可触发动画notifyItemRangeInserted(positionStart, itemCount)。这些调用将触发Animator,调用notifyDatasetChanged() 不会。
触发插入动画:
recyclerView.getAdapter().notifyItemInserted(position);
recyclerView.getAdapter().notifyItemRangeInserted(positionStart, itemCount);
Run Code Online (Sandbox Code Playgroud)
希望这段代码对您有帮助!
创建一个动画文件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>
Run Code Online (Sandbox Code Playgroud)
在活动中
Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.animation_from_right);
holder.itemView.startAnimation(animation);
Run Code Online (Sandbox Code Playgroud)
在 onBindViewHolder 的 Adapter 中使用上面的代码