Android - PageView in new BottomNavigationBar - prevent reload fragments

。_饼干妹妹 提交于 2021-02-08 04:43:23

问题


In my StartActivity the BottomNavigationBar Listener has the following setup:

private GuideFragment guideFragment = new GuideFragment();
private MapFragment mapFragment = new MapFragment();
private MoreFragment moreFragment = new MoreFragment();

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.navigation_guide:
                selectedFragment = guideFragment;
                break;
            case R.id.navigation_map:
                selectedFragment = mapFragment;
                break;
            case R.id.navigation_more:
                selectedFragment = moreFragment;
                break;
        }
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.content, selectedFragment);
        transaction.commit();
        return true;
    }

};

As I mentioned above I want to prevent that the selected fragments always reloads the sources/view. I tried out some stuff like - in the fragments:

if (rootView == null)
     inflater.inflate...

But the fragments still recreate the view and load (in my case) webresources new.

I read something that a PageView could help, especially

offScreenPageLimit

should do the magic.

My main question is where should I implement a PageViewer - Is it possible in my StartActivity? Or can I solve the problem in an other way?


回答1:


I did it boys! There is no ViewPager necessary.

Here is my solution (all coded in StartActivity not in Fragments):

private final GuideFragment guideFragment = new GuideFragment();
private final MapFragment mapFragment = new MapFragment();
private final MoreFragment moreFragment = new MoreFragment();
private final android.app.FragmentManager fm = getFragmentManager();
Fragment active = guideFragment;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_guide:
                if(active != guideFragment) {
                    fm.beginTransaction().show(guideFragment).commit();
                }
                else {
                    fm.beginTransaction().hide(active).show(guideFragment).commit();
                }
                active = guideFragment;
                break;
            case R.id.navigation_map:
                fm.beginTransaction().hide(active).show(mapFragment).commit();
                active = mapFragment;
                break;
            case R.id.navigation_more:
                fm.beginTransaction().hide(active).show(moreFragment).commit();
                active = moreFragment;
                break;
        }

        return true;
    }

};

and in onCreate list the transaction commits.

fm.beginTransaction().add(R.id.content,moreFragment).commit();
fm.beginTransaction().add(R.id.content, mapFragment).commit();
fm.beginTransaction().add(R.id.content, guideFragment).commit();

It is very important to commit the first tabs fragment last(fragm3,fragm2,fragm1) if you have 3 tabs.

Highly speed performance on the smartphone now by not loading every fragment new/refresh.



来源:https://stackoverflow.com/questions/44375904/android-pageview-in-new-bottomnavigationbar-prevent-reload-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!