我有一個recyclerView
onCreateView
方法。但我必須填寫我以onResume
方法收到的價值。問題是onCreateView
在onResume
之前開始。我能做什麼?獲取值onResume方法
0
A
回答
1
onCreateView位於適配器中,您可以在onResume中創建適配器, 然後setAdapter
用於onResume中的recyclerview。
此外,爲了避免每次使用某種布爾型本地變量時都會創建它,它會告訴您該適配器是否已創建。
@Override
protected void onResume() {
super.onResume();
if (adapterNotCreated) {
adapter = new RecyclerAdapter(this, data)
daysRecyclerView.setAdapter(adapter);
daysRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
}
}
就是這樣的。
0
應該通過onResune調用notifidatasetchsnged更新,而不是setAdapter,應在onCreateView做
發表您的'Fragment'代碼 – Sanoop
您可以創建一個全球性的Recyclerview實例,並在onCretaeView初始化適配器的數據,並將值放入片段生命週期的Resume中。 – Saurabh
postAdapter.notifyDataSetChanged();在正確的地方? – EagleEye