删除项目后StaggeredGridLayout管理器,项目分散

时间:2017-09-23 06:20:39

标签: android staggeredgridlayoutmanager

我正在使用交错网格布局从数据库向回收者视图显示数据。我遇到了一个问题,从db中删除项目以及从适配器中删除位置后,我得到了一些项目渲染问题。像散乱的所有在这个地方。 这是我的代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_favorite);
    ButterKnife.bind(this);
    toolbar = (Toolbar) findViewById(R.id.fav_note_toolbar);
    setSupportActionBar(toolbar);

    noteAdapter = new NoteAdapter(noteModelList, NoteFavoriteActivity.this);
    layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(noteAdapter);
    touchListenerRecycler();
    loadDataAll();


}
private void loadAll() {
    noteModelList.clear();

    DBManagerFav dbManagerFav = DBManagerFav.getInstance(NoteFavoriteActivity.this);
    dbManagerFav.openDataBase();
    noteModelList = dbManagerFav.getAllNoteList();
    Log.i(TAG, " size : " + noteModelList.size());
    noteAdapter = new NoteAdapter(noteModelList, NoteFavoriteActivity.this);
    recyclerView.setAdapter(noteAdapter);
    noteAdapter.notifyDataSetChanged();

    dbManagerFav.closeDataBase();

}


 private void deleteOperation() {

    DBManagerFav dbManagerFav = DBManagerFav.getInstance(NoteFavoriteActivity.this);
    dbManagerFav.openDataBase();

    NoteModel noteModel = new NoteModel();
    noteModel.setId(noteModelList.get(adapterClickedPosition).getId());

    int status = dbManagerFav.deleteNote(noteModelList.get(adapterClickedPosition).getId());
    if (status > 0) {

        noteAdapter.removeAt(adapterClickedPosition);

    }
    dbManagerFav.closeDataBase();
    loadDataAll();
}
//this belongs to adapter
public void removeAt(int position) {
    Log.d(TAG, " removing at position : " + position);
    noteModelList.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, noteModelList.size());
    notifyDataSetChanged();
}

我之前和之后附加了两个截图 删除项目之前

enter image description here

删除项目后

enter image description here

你能指出我还需要什么吗?

1 个答案:

答案 0 :(得分:0)

你可以放入loadDataAll(){

    layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);

// ........ } 瞧..