2017-09-23 61 views
2

我正在使用交錯的網格佈局顯示數據,以從數據庫回收視圖。我遇到了一個問題,從db刪除項目以及從適配器刪除位置後,我得到一些物品渲染問題。像散落在各處。 這裏是我的代碼StaggeredGridLayout管理器刪除項目後,項目分散

@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

你能指出前後我出了什麼我需要什麼?

+0

刪除'notifyDataSetChanged();'從** RemoveAt移除( int position)**方法 –

+0

@Nilesh,感謝回覆,讓我快速瀏覽 – Avinash

+0

@Nilesh,它不起作用 – Avinash

回答

0

你可以在裏面放loadDataAll(){

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

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

+0

謝謝它的工作原理,我試圖與invalidateSpan但我錯過了這個通知佈局後重新加載 – Avinash