Interaction between two fragments



  • In ViewPager, I set two fragments.

    Adapter:

    public class ViewPagerAdapter extends FragmentPagerAdapter
    {
    
    ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> titles = new ArrayList<>();
    
    public ViewPagerAdapter(FragmentManager fm)
    {
        super(fm);
    }
    
    public void addFragments(Fragment fragment, String title)
    {
        this.fragments.add(fragment);
        this.titles.add(title);
    }
    
    @Override
    public Fragment getItem(int position)
    {
        return fragments.get(position);
    }
    
    @Override
    public int getCount()
    {
        return fragments.size();
    }
    
    @Override
    public CharSequence getPageTitle (int position)
    {
        return titles.get(position);
    }
    

    }

    Setting an adapter in ViewPager.

    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

    viewPagerAdapter.addFragments(new ListWordFragment(), "Список");
    viewPagerAdapter.addFragments(new AddWordFragment(), "Добавить слово");
    
    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);
    

    Thus, the first part of it contains a sheet and the second form to fill where, when pressed on the button, the word which is inserted in the edit text should appear in the first fragment, i.e. in the sheet. And how do you do this interaction between two fragments?



  • You don't have any fragments on your own. They exist within the framework of an activist. The interaction between the fragments is organized with the assistance of this Activity. However, the fragment is an interface that will be used by the Assymites.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2