目錄
問題內容
解決方法
首頁 Java 如何從另一個fragment取得recycleView的fragment onViewCreated中的資料?

如何從另一個fragment取得recycleView的fragment onViewCreated中的資料?

Feb 11, 2024 pm 05:51 PM

在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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1667
14
CakePHP 教程
1426
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1255
24