K
K
kirawa2018-03-14 16:44:56
Android
kirawa, 2018-03-14 16:44:56

How to properly work with floatingActionButton in viewPager? What am I doing wrong?

Good afternoon! I have a viewPager with a FragmentStatePagerAdapter, at the viewPager level I created a FloatingActionButton in xml. The viewPager has items with the same type of content and the items are just a fragment that is filled depending on the position of the viewPager.

private class ViewPagerAdapter extends FragmentStatePagerAdapter {
  @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            bundle.putInt(POSITION, position);
            ItemViewPagerFragment fragment =  new ItemViewPagerFragment();
            fragment.setArguments(bundle);
            return fragment;
        }
}

In the ItemViewPagerFragment , I find a FloatingActionButton and click on it.
FloatingActionButton floatingActionButton = getActivity().findViewById(R.id.fab);
        floatingActionButton.setOnClickListener(this);

By clicking on the floatingActionButton, another fragment should be launched, depending on the position of the viewPager, for example:
private void startNewVisitDoc() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null)
                .replace(R.id.contentMain, new ClientViewPagerContainer())
                .commit();
    }

During initialization, if I move to the second position of the viewPager, and then return to the first position again, then getFragmentManager() == null, and childFragmentManager ==
null which works correctly.
The application itself crashes. Help me please. What have I done wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question