2

如何限制对 BottomNavigatioView 项目的点击一次,以便当有人向该项目发送垃圾邮件时我的 mapfragment 不必重新加载?

我目前的解决方案是这个:

fragmentManager = getSupportFragmentManager();
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        Fragment currentFragment = fragmentManager.findFragmentById(R.id.main_container);
        int id = item.getItemId();
        switch (id){
            case R.id.World:
                if (!(currentFragment instanceof MapsFragment)) {
                fragment = new MapsFragment();
                    }
                break;

            case R.id.Friends:
                fragment = new FriendsFragment();
                break;


            case R.id.Chats:
                fragment = new You_Fragment();
                break;
        }
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.main_container, fragment).commit();
        return true;
    }
});

但它不起作用,因为片段抛出了一个 Nullpointer。

我可以改变什么来实现上述目标?

4

1 回答 1

1

尝试这个!为我工作:

fragmentManager = getSupportFragmentManager();
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment fragment = null;
    Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.main_container);
    int id = item.getItemId();
    switch (id){
        case R.id.World:
            if (currentFragment instanceof MapsFragment) {

                return false;

            } else {

                fragment = new MapsFragment();

            }
            break;

        case R.id.Friends:
            fragment = new FriendsFragment();
            break;


        case R.id.Chats:
            fragment = new You_Fragment();
            break;
    }
    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.main_container, fragment).commit();
    return true;
    }
});
于 2017-03-12T08:23:38.497 回答