編輯部:
你好。
我有一個MainActivity,我在上面打開Fragment A:
fragmentTransaction = getSupportFragmentManager().beginTransaction()。
fragmentTransaction.replace(R.id.db_container, Fragment A)。
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)。
fragmentTransaction.commit()。
這個片段使用LiveData并顯示RecyclerView串列。
然后在用戶選擇一些類別后,我打開片段B:
fragmentTransaction.hide(Fragment A)。
fragmentTransaction.add(R.id.db_container, Fragment B);
fragmentTransaction.addToBackStack(null)。
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)。
fragmentTransaction.commit()。
這個片段也使用了LiveData并顯示了RecyclerView串列。
當用戶選擇一個單一的專案并想編輯它時,他們被移到EditItem Activity。
在用戶做了一些修改并保存后,該活動以命令結束。finish();
用戶回到Frist>活動。
用戶回到片段B,RecyclerView串列被更新。
問題是,在片段B的串列被更新之前,片段A的串列首先被更新。
確切地說,片段A中的代碼:
item_viewmodel.getAllCategoryModel()。 observe(getViewLifecycleOwner(), new Observer< List<Items>>(){
@Override
public void onChanged(List< Items> myLists) {
//這里的代碼是在更新片段B中的串列之前先執行的。
}
});
那么問題來了。
如何只更新片段B中的串列?
后來我將從片段B中手動更新片段A中的串列,使用interface回呼。
另外,我想先更新片段B中的串列,然后再更新片段A中的串列。
如果可以的話,我不想洗掉片段A,只想隱藏它。
uj5u.com熱心網友回復:
正如我所理解的,你只想更新片段B,然后手動更新片段A。
我假設你在串列中使用的資料是在片段A和B之間共享的,這就是為什么片段A會被更新,因為A和B對相同的物件有觀察者。
你可以在A中使用一個布爾變數boolean shouldUpdate = true;。
并在你移動到B時使其為假。
同時在A中的觀察者中檢查if(shouldUpdate) .
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318391.html
標籤:
上一篇:從片段中改變活動的按鈕狀態
