简体   繁体   English

RecyclerView-NotifyItemInsert上没有动画

[英]RecyclerView - No animation on NotifyItemInsert

For some reason, when adding a new item to the RecyclerView (should be inserted to the top of the list), it won't show up unless I scroll down the list and back up to the top, and without any animation either. 出于某种原因,当将新项目添加到RecyclerView时(应插入到列表的顶部),除非我向下滚动列表并返回顶部,并且没有任何动画,否则它不会显示。 (Just appears at the top of the list as if it was there the whole time). (就像出现在整个列表中一样,它刚出现在列表的顶部)。 Removing an item works fine with the proper animations. 删除项目可以正常播放动画。

RecyclerViewAdapter: RecyclerViewAdapter:

@Override
public void onNewDatabaseEntryAdded() {
    //item added to top of the list
    notifyItemInserted(0);
}

public FileViewerAdapter(Context context) {
    super();
    mContext = context;
    mDatabase = new DBHelper(mContext);
    mDatabase.setOnDatabaseChangedListener(this);
}

SQLite Database: SQLite数据库:

private static OnDatabaseChangedListener mOnDatabaseChangedListener;

public static void setOnDatabaseChangedListener(OnDatabaseChangedListener listener) {
    mOnDatabaseChangedListener = listener;
}

public long addRecording(String recordingName, String filePath, long length) {

    SQLiteDatabase db = getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(DBHelperItem.COLUMN_NAME_RECORDING_NAME, recordingName);
    cv.put(DBHelperItem.COLUMN_NAME_RECORDING_FILE_PATH, filePath);
    cv.put(DBHelperItem.COLUMN_NAME_RECORDING_LENGTH, length);
    cv.put(DBHelperItem.COLUMN_NAME_TIME_ADDED, System.currentTimeMillis());
    long rowId = db.insert(DBHelperItem.TABLE_NAME, null, cv);

    if (mOnDatabaseChangedListener != null) {
        mOnDatabaseChangedListener.onNewDatabaseEntryAdded();
    }

    return rowId;
}

Listener: 听众:

public interface OnDatabaseChangedListener{
    void onNewDatabaseEntryAdded();
    void onDatabaseEntryRenamed();
}

edit: 编辑:

I should mention that if I use NotifyDataSetChanged instead of NotifyItemInserted, then the new item shows up immediately, but the RecyclerView will not scroll to the top of the list. 我应该提到,如果我使用NotifyDataSetChanged而不是NotifyItemInserted,则新项目将立即显示,但是RecyclerView不会滚动到列表的顶部。 (Manually have to scroll up to see it). (必须手动向上滚动才能看到它)。

This is happening because LinearLayoutManager thinks the item is inserted above the first item. 发生这种情况是因为LinearLayoutManager认为该项目已插入到第一个项目的上方。 This behavior makes sense when you are not at the top of the list but I understand that it is unintuitive when you are at the top of the list. 当您不在列表顶部时,此行为是有道理的,但我知道当您在列表顶部时,这是不直观的。 We may change this behavior, meanwhile, you can call linearLayoutManager.scrollToPosition(0) after inserting the item if linearLayoutManager.findFirstCompletelyVisibleItemPosition() returns 0. 我们可能会更改此行为,同时,如果linearLayoutManager.findFirstCompletelyVisibleItemPosition()返回0,则可以在插入项目后调用linearLayoutManager.scrollToPosition(0)

I fix it using notifyItemChange first item. 我使用notifyItemChange第一项修复它。 Just like this: 像这样:

((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(0, 0);
adapter.notifyItemChanged(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM