如何在 fab 中更改图像

How to Change Image in fab

当我改变 fragment 我的 FAB (FloatingActionButton) 不会改变她的形象。我有这个方法来改变图像:

private void fabImages(){
    final NavHostFragment nhf = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main);
    if(nhf != null){
        Fragment fragmentActual = nhf.getChildFragmentManager().getFragments().get(0);
        if(fragmentActual instanceof  HomeFragment){
            binding.appBarMain.fab.setImageResource(R.drawable.ic_add_auto_blanco);
        }else if(fragmentActual instanceof GalleryFragment){
            binding.appBarMain.fab.setImageResource(R.drawable.ic_save);
        }
    }
}

我正在将此 activity 用于我的应用程序:

我在我的 MainActivity onCreateonResume 中应用了此方法,但图像没有改变,为什么?

更新:

MainActivity:

public class MainActivity extends AppCompatActivity{
    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;
    private String horaString, fechaString;
    private NombreTallerPreferencia nombreTallerPreferencia;
    private PermissionHelper permissionHelper;
    private static final int PICK_IMAGE = 1;

    public static final String TAG = "logcat";
    public static final String BARRA = "/";

    private View view_imagen_perfil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);

        fabImages(); //method to change image

        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    protected void onResume() {
        super.onResume();
        fabImages(); //method to change image
    }

您的应用程序似乎没有像 else 这样的回退条件,只是因为您的值为空。 您可以在此处阅读有关更改 FAB 图标的更多信息 或来自官方 android 工作室文档 https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html

问题不在于如何将可绘制对象设置为 FAB,因为您已经做对了。但是问题出在 onResume().

的地方

场景如下:

  • 应用启动,onResume() 被触发,fabImages() 被调用,所以,它是 HomeFragment,所以设置 R.drawable.ic_add_auto_blanco,这对你有用。
  • 你去GalleryFragment,所以HomeFragment现在隐藏了,GalleryFragment显示了,但是这里activity的onResume()不会'没有被调用,因为 activity 没有被暂停(),即 onPause() 没有被调用;
  • 当您在 GalleryFragment 时,如果您通过旋转设备更改配置,则 onResume() 将被调用,因此 fabImages() 并且 fab 更改其可绘制对象至 R.drawable.ic_save.

因此,您需要在 activity 的 onResume 以外的其他地方调用 fabImages()

要解决此问题,您需要在 activity 中注册一个侦听器,以便在导航更改其当前目的地时进行跟踪,并通过 OnDestinationChangedListener 界面进行跟踪:

所以,在你的 activity 的 onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    setSupportActionBar(binding.appBarMain.toolbar);

    fabImages(); //method to change image

    DrawerLayout drawer = binding.drawerLayout;
    NavigationView navigationView = binding.navView;
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
    
    
    
    /////////////////////////
    // Here is the new code:
    /////////////////////////
    
    NavController.OnDestinationChangedListener destinationChangedListener = new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
            
            if (destination.getId() == R.id.nav_home) {
                binding.appBarMain.fab.setImageResource(R.drawable.ic_add_auto_blanco);

            } else if (destination.getId() == R.id.nav_gallery) {
                binding.appBarMain.fab.setImageResource(R.drawable.ic_save);
            }
        }
    };

    navController.addOnDestinationChangedListener(destinationChangedListener);
    
}