如何從另一個類中刪除RecyclerView中的項目?我想避免使用靜態方法,因爲我最終創建了很多它們,這是一個錯誤的過程。還有其他解決方案嗎?RecyclerView從另一個類中刪除一個項目
我想將其從BottomSheetFragment中移除。
如何從另一個類中刪除RecyclerView中的項目?我想避免使用靜態方法,因爲我最終創建了很多它們,這是一個錯誤的過程。還有其他解決方案嗎?RecyclerView從另一個類中刪除一個項目
我想將其從BottomSheetFragment中移除。
您可以使用Life Cycle了點。你所要做的就是創建一個通信接口。
interface RemoveItemSignal{
void onRemove();
}
然後註冊一個監聽器。
Signal<RemoveItemSignal> signal = SignalBag.Inject(RemoveItemSignal.class);
signal.addListener(this); // Your listener that implements RemoveItemSignal
從你的片段,你可以派遣這個信號是這樣的:
Signal<RemoveItemSignal> signal = SignalBag.Inject(RemoveItemSignal.class);
signal.dispatcher.onRemove();
您是否閱讀過文檔communicating with other fragments? 它建議在BottomSheetFragment
內部創建偵聽器接口,該接口將負責來自它的操作。比你activity
應該實現此類似這樣的
public static class MainActivity extends Activity
implements BottomSheetFragment.OnActionSelectedListener{
...
public void onActionSelected(int position) {
// The user did some action from the BottomSheetFragment
// Do something here to remove item from the RecyclerView
}
}