Restoring fragment state when changing fragments through bottom navigation bar

后端 未结 4 688
星月不相逢
星月不相逢 2020-12-24 09:22

I have bottom navigation bar on click of item in navigation bar i am replacing fragments. I have 3 fragments A,B,C so on click of b item B fragment is loaded and in B i am c

4条回答
  •  时光说笑
    2020-12-24 09:46

    You should use ViewPager2 as it is the updated version of ViewPager.

    Step by step guide (to restore/retain an EditText's state as an example):

    Step 1:

    Add dependencies:

    dependencies {
    
        def nav_version = "2.3.0"
        implementation "androidx.navigation:navigation-fragment:$nav_version"
        implementation "androidx.navigation:navigation-ui:$nav_version"
    
        implementation 'androidx.viewpager2:viewpager2:1.0.0'
    
    }
    

    Step 2:

    Add menu_bottom_navigation.xml to res/menu: (You may also add icons to menu items)

    
    
        
        
    
    
    

    Step 3:

    Add activity_main.xml to res/layout: (adding menu to BottomNavigationView and placing ViewPager2)

    
    
    
        
    
        
    
    
    

    Step 4:

    Add fragment_first.xml to res/layout:

    
    
        
    
        
    
    
    
    

    Step 5:

    Add fragment_second.xml to res/layout:

    
    
        
    
        
    
    
    

    Step 6:

    ViewPagerAdapter.java:

    public class ViewPagerAdapter extends FragmentStateAdapter {
        private final List mFragmentList = new ArrayList<>();
    
        public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, Lifecycle b ) {
            super(fragmentManager,b);
        }
    
        public void addFragment(Fragment fragment) {
            mFragmentList.add(fragment);
        }
    
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return mFragmentList.get(position);
        }
    
        @Override
        public int getItemCount() {
            return mFragmentList.size();
        }
    
    }
    

    Step 7:

    FirstFragment.java:

    public class FirstFragment extends Fragment {
    
    
        public FirstFragment() {
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_first, container, false);
        }
    
    }
    

    Step 8:

    SecondFragment.java:

    public class SecondFragment extends Fragment {
    
    
        public SecondFragment() {
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_second, container, false);
        }
    
    }
    

    Step 9:

    MainActivity.java:

    public class MainActivity extends AppCompatActivity {
    
        BottomNavigationView bottomNavigationView;
    
        private ViewPager2 viewPager2;
    
        FirstFragment firstFragment;
        SecondFragment secondFragment;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            viewPager2 = findViewById(R.id.viewpager2);
            bottomNavigationView = findViewById(R.id.bottom_navigation);
    
            bottomNavigationView.setOnNavigationItemSelectedListener(
                    new BottomNavigationView.OnNavigationItemSelectedListener() {
                        @Override
                        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.menu_first:
                                    viewPager2.setCurrentItem(0,false);
                                    break;
                                case R.id.menu_second:
                                    viewPager2.setCurrentItem(1,false);
                                    break;
                            }
                            return false;
                        }
                    });
    
            viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    super.onPageScrolled(position, positionOffset, positionOffsetPixels);
    
                    switch (position) {
                        case 0:
                            bottomNavigationView.getMenu().findItem(R.id.menu_first).setChecked(true);
                            break;
                        case 1:
                            bottomNavigationView.getMenu().findItem(R.id.menu_second).setChecked(true);
                            break;
                    }
                }
            });
    
            setupViewPager(viewPager2);
    
        }
    
        private void setupViewPager(ViewPager2 viewPager) {
    
            ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), getLifecycle());
    
            firstFragment =new FirstFragment();
            secondFragment =new SecondFragment();
    
            adapter.addFragment(firstFragment);
            adapter.addFragment(secondFragment);
    
            viewPager.setAdapter(adapter);
        }
    
    }
    

提交回复
热议问题