2

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

 @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();
}

我在删除项目之前和之后附上了两张截图

在此处输入图像描述

删除项目后

在此处输入图像描述

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

4

1 回答 1

0

您可以放入 loadDataAll(){

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

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

于 2017-09-23T07:31:33.227 回答