Home  >  Article  >  How to get the data in recycleView's fragment onViewCreated from another fragment?

How to get the data in recycleView's fragment onViewCreated from another fragment?

王林
王林forward
2024-02-11 17:51:09478browse

In Android development, Fragment is an important component used to build flexible user interfaces. When using Fragment, sometimes we need to obtain data from one Fragment in another Fragment. Then in the onViewCreated method of the fragment, we can obtain the data in the RecyclerView in other Fragments through the findViewById method. First, we need to make sure that the RecyclerView has been initialized and filled with data before getting the data. Then, we can get the instance of the target Fragment through the findFragmentById method of FragmentManager, and then get the instance of RecyclerView through the findViewById method of the instance. Finally, we can get the data through the RecyclerView adapter. In this way, we can get the data of RecyclerView in another Fragment in the current Fragment. This is a simple and effective method that can help us better handle the transfer and sharing of data in Android development.

Question content

I have fragment workoutfragment and bundle for transferring data to fragment delitefragment:

fragmentmanager fragmentmanager = getfragmentmanager();
 fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();
 fragmenttransaction.replace(r.id.fragment_container, new delitefragment());
 fragmenttransaction.commit();

In the delitefragment, I have an onviewcreated method where the recycler view data call is locked.

@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);
   }

How to get data in onviewcreated namedaytrain? (so it will go to recyclerview adapter)

Workaround

Use AndroidX view model library to communicate with your activity and fragments. It's very simple✍️

here: AndroidX Jetpack ViewModel

The above is the detailed content of How to get the data in recycleView's fragment onViewCreated from another fragment?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete