Communication between nested fragments in Android

前端 未结 4 1247
无人共我
无人共我 2020-12-07 17:00

I recently learned how to make nested fragments in Android. I don\'t know how communication is supposed to happen, though.

From reading the fragment commun

4条回答
  •  抹茶落季
    2020-12-07 17:44

    Using ViewModel to communicate between nested Fragments

    With pieces of the old ViewModel and LiveData now deprecated. I would like to provide information on this process as it currently stands.

    In this example I am using a the predefined TabLayout that can be selected when one creates a project.

    Here I have a TextView in the parent fragment -- tab, with an EditText and a Button in the child fragment -- custom layout. It passes the text entered in the child fragment to the field in the parent fragment.

    I hope this helps someone.

    ViewModel Class

    public class FragViewModel extends ViewModel {
      private MutableLiveData digit = new MutableLiveData<>();
    
      public void insertDigit(CharSequence inDigit){
          digit.setValue(inDigit);
      }
    
      public LiveData getDigit(){
          return digit;
      }
    }
    

    This is, for the most part, verbatim from the Android Developers ViewModel Overview with a small variable name change. The Overview was used as a guide for the parent and child code below.

    The Parent Fragement Code

    public class Tab0Main extends Fragment {
    
    private FragViewModel model0;
    
    // This is the Child Fragment.
    private ChildFrag childFrag;
    
    private TextView textView;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab0_main, container, false);
    
        // Setup the text field.
        textView = view.findViewById(R.id.tab0TextView);
    
        // Inserting the Child Fragment into the FrameLayout.
        childFrag = new ChildFrag();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.tab0Frame, childFrag).commit();
    
        return view;
    }
    
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        model0 = new ViewModelProvider(getActivity()).get(FragViewModel.class);
        model0.getDigit().observe(getViewLifecycleOwner(), new Observer() {
            @Override
            public void onChanged(CharSequence charSequence) {
                textView.setText(charSequence);
            }
        });
    }
    

    The Child Fragment Code

    public class ChildFrag extends Fragment {
    
    private EditText fragEditText;
    private Button fragButton;
    
    // The ViewModel declaration
    FragViewModel model;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.child_frag_layout, container, false);
    
        // The ViewModel Instantiation
        model = new ViewModelProvider(getActivity()).get(FragViewModel.class);
    
        fragEditText = view.findViewById(R.id.fragEditText);
        fragButton = view.findViewById(R.id.fragButton);
        fragButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence in = fragEditText.getText();
                // Inserting a digit into the ViewModel carrier.
                model.insertDigit(in);
            }
        });
        return view;
      }
    }
    


提交回复
热议问题