在Android開發中,Fragment是一個重要的元件,用於建立靈活的使用者介面。在使用Fragment時,有時候我們需要從一個Fragment中取得另一個Fragment中的資料。那麼在fragment的onViewCreated方法中,我們可以透過findViewById方法來取得其他Fragment中的RecyclerView中的資料。首先,我們需要確保在獲取資料之前,RecyclerView已經被初始化並填充了資料。然後,我們可以透過FragmentManager的findFragmentById方法來取得目標Fragment的實例,再透過實例的findViewById方法來取得RecyclerView的實例。最後,我們可以透過RecyclerView的適配器來取得資料。這樣,我們就可以在目前的Fragment中取得到另一個Fragment中RecyclerView的資料了。這是一個簡單而有效的方法,可以幫助我們在Android開發中更好地處理資料的傳遞和共享。
問題內容
我有片段 workoutfragment 和 bundle,用於將資料傳輸到片段 delitefragment:
fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.fragment_container, new delitefragment()); fragmenttransaction.commit();
在 delitefragment 中,我有一個 onviewcreated 方法,其中回收器視圖資料呼叫被鎖定。
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //******************************** getting data from DeliteFragment getParentFragmentManager().setFragmentResultListener("text from WOF to AFJ", this, new FragmentResultListener() { @Override public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle textBundle) { Integer number_workout_train_position = textBundle.getInt("text from WOF to AFJ"); String nameDayTrain = new CardSourceImplDayTrain(getActivity()).getCardData(number_workout_train_position).getTitle().toString(); } }); String nameDayTrainRW = nameDayTrain; recyclerView = view.findViewById(R.id.train_delite_recycleView); cardSource = new CardSourceImplDelite(getActivity().getApplicationContext(),nameDayTrainRW,nameDayTrainRW); adapter = new itemAdapterDelite(cardSource); }
如何在 onviewcreated 中取得資料 namedaytrain? (所以它將轉到recyclerview適配器)
解決方法
使用 AndroidX 視圖模型庫與您的 Activity 和片段進行通訊。這非常簡單✍️
這裡: AndroidX Jetpack ViewModel
以上是如何從另一個fragment取得recycleView的fragment onViewCreated中的資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。