首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RecyclierView的notifyItemInserted不能与arrayList一起使用

RecyclierView的notifyItemInserted不能与arrayList一起使用
EN

Stack Overflow用户
提问于 2016-07-10 22:30:11
回答 0查看 2.4K关注 0票数 0

我有一个ArrayList和一个RecyclerView。从DB中检索数据并将其存储在ArrayList中,以便在RecyclerView中显示。将新项添加到RecyclerView中时,所有操作都可以正常工作,但没有添加动画。我知道我应该使用notifyItemInserted来添加动画,但它不起作用。未显示插入动画。现在,我必须返回到前一页,然后再次进入该页,以便显示添加的项目。那么,如何重新添加插入动画呢?

任何帮助都将不胜感激。谢谢。

传递数据和设置适配器的代码:

代码语言:javascript
复制
 db = new DatabaseHelper(this);
    dbList = new ArrayList<>();
    dbList = db.getFilteredItems();

    RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);

    //newest to oldest order (database stores from oldest to newest)
    llm.setReverseLayout(true);
    llm.setStackFromEnd(true);

    mRecyclerView.setLayoutManager(llm);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    adapter = new RecyclerAdapter(this, llm, dbList);
    mRecyclerView.setAdapter(adapter);

从DB检索数据的代码:

代码语言:javascript
复制
 //retrieve filtered data from DB
public List<AudioItem> getFilteredItems(){
    List<AudioItem> audioList = new ArrayList<>();
    String titleName = EditActivity.titleName;
    String query = "select * from " + TABLE_NAME + " where " + COLUMN_NAME_RECORDING_NAME + " like '" + titleName + "%'";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query,null);

    if (cursor.moveToFirst()){
        do {
            AudioItem audio = new AudioItem();
            audio.setId(Integer.parseInt(cursor.getString(0)));
            audio.setName(cursor.getString(1));
            audio.setFilePath(cursor.getString(2));
            audio.setLength(Integer.parseInt(cursor.getString(3)));
            audio.setTime(Long.parseLong(cursor.getString(4)));
            audioList.add(audio);

        }while (cursor.moveToNext());
        cursor.close();
    }

    return audioList;
}

将数据插入数据库的代码:

代码语言:javascript
复制
/* Insert data into database */
public void addRecording(String recordingName, String filePath, long length) {

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

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

调用插入动画的代码:

代码语言:javascript
复制
 @Override
public void onNewDatabaseEntryAdded() {
    //item added to top of the list
    Log.e("Count: ", Integer.toString(getItemCount()));
   // notifyDataSetChanged();
    notifyItemInserted(getItemCount());
    //llm.scrollToPosition(getItemCount() - 1);
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38292967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档