通过底部导航视图停止刷新

时间:2019-06-08 04:13:51

标签: android bottomnavigationview

这是一个简单的问题。我正在为我的应用程序使用BottomNavigationView(bnv),并且有5个菜单。每个菜单都在bnv顶部的活动上显示视图。每个菜单都从服务器接收信息。但是,每次我用bnv更改视图时,它都会不断刷新,因为会再次调用onCreateView。我想停止此操作,因为每次更改bnv时,访问服务器时,服务器会占用大量服务器

有什么方法可以控制此方法,以便仅在更改提供给视图的信息时才进行刷新?

我只是添加我的bnv代码,以防万一。

我的onCreate部分。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    backHandler = new BackPressedHandler(this);

    dialog = new ProgressDialog(MainActivity.this);


    toolbar = findViewById(R.id.main_toolBar);
    bottomNavView = findViewById(R.id.bottomNavView);

    setSupportActionBar(toolbar);

    manager = getSupportFragmentManager();

    mainAccident = new Main_AccidentFragment();
    mainCargo = new Main_CargoFragment();
    mainCarManager = new Main_CarManagerFragment();
    mainCommunity = new Main_CommunityFragment();
    mainMyCar = new Main_MyCarFragment();

    manager.beginTransaction().add(R.id.frameMainActivity, mainMyCar).commit();

    bottomNavView.setSelectedItemId(R.id.bottomBtnMyCar);
    bottomNavView.setOnNavigationItemSelectedListener(listener);

    new ReceiveMainAsyncTask().execute();

}

还有bnv侦听器部分。

private BottomNavigationView.OnNavigationItemSelectedListener listener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        int id = menuItem.getItemId();

        FragmentTransaction transaction = manager.beginTransaction();

        transaction.addToBackStack(null);

        switch (id) {
            case R.id.bottomBtnMyCar:
                transaction.replace(R.id.frameMainActivity, mainMyCar).commit();
                break;

            case R.id.bottomBtnCarManager:
                transaction.replace(R.id.frameMainActivity, mainCarManager).commit();
                break;
            case R.id.bottomBtnCargo:
                transaction.replace(R.id.frameMainActivity, mainCargo).commit();
                break;
            case R.id.bottomBtnCommunity:
                transaction.replace(R.id.frameMainActivity, mainCommunity).commit();
                break;
            case R.id.bottomBtnAccident:
                transaction.replace(R.id.frameMainActivity, mainAccident).commit();
                break;
        }

        return true;

    }
};

0 个答案:

没有答案