我有一个 RecyclerView 并将项目添加到索引 0 处的 mCommentArrayList
。当新项目 (CardView) 添加到 RecyclerView 时,我试图在 View 顶部创建一个滑入式动画.
我知道有可以使用的库,我什至探索过https://github.com/wasabeef/recyclerview-animators .但是,文档是有限的,我不确定该采取什么方法。
请注意,我将所有新项目添加到 index 0
处的 mCommentArrayList
,以便它们显示在 View 的顶部。我知道在适配器中有一些工作要做,特别是 onBindViewHolder()
,但我不知道要放什么才能激 Activity 画。
我第一次调用 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) {
}
});
在数据更改时对 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);
}
}
}
最佳答案
使用您正在讨论的库 (https://github.com/wasabeef/recyclerview-animators) 可以很容易地将 SlideInAnimator
添加到您的 RecyclerView
。只需使用以下代码为您的 RecyclerView
设置一个 Animator
(选择一个):
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);
关于java - 如何为新项目添加 RecyclerView 幻灯片动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444739/