The idea is elementary, hide the fab if the appbar in a hidden state. Added onOffsetChanged to track appbar status. There is a layout in the activity. There is a TextView pair in it and a button that switches the view to the GONE state. Why hiding fab occurs only when TextView is in a hidden state, and in the case when they are VISIBLE function of hiding fab does not react at all? By experience, I realized that the problem is in the asynchronous data loading code from the database, if you disable loading and leave empty TextView hiding the fab works correctly in the case of hidden views and visible ones. I want to note that if you scroll the screen and hide the appbar when the TextView visible, the fab will not disappear, but if you press the button in the same state and hide the TextView , then the fab immediately hidden. I just can’t understand the essence of what hinders the execution of the code, because it’s just asynchronous loading from the database that doesn’t have any influence whatsoever. enter image description here

 public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { DataBase db; SharedPreferences pref; public static final String APP_F = "f" ; boolean set1; // переменная состояния открытости/закрытости блока private static final int NOTIFY_ID = 0; TextView num_1; TextView item1; float num1; FloatingActionButton fab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pref = PreferenceManager.getDefaultSharedPreferences(this); Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mActionBarToolbar); db = new DataBase(this); db.open(); item1 = (TextView) findViewById(R.id.last_add); num_1 = (TextView) findViewById(R.id.num_1); fab = (FloatingActionButton) findViewById(R.id.fab1); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); if (!pref.getBoolean(APP_F, set1)) { item1.setVisibility(GONE); num_1.setVisibility(GONE); } hideFab(); getSupportLoaderManager().initLoader(1, null, this); getSupportLoaderManager().getLoader(1).forceLoad(); } public void set_max_min (View view) { if (item1.getVisibility() == View.VISIBLE) { item1.setVisibility(GONE); num_1.setVisibility(GONE); set1 = false; SharedPreferences.Editor editor = pref.edit(); editor.putBoolean(APP_F, set1); editor.apply(); } else { item1.setVisibility(View.VISIBLE); num_1.setVisibility(View.VISIBLE); set1 = true; SharedPreferences.Editor editor = pref.edit(); editor.putBoolean(APP_F, set1); editor.apply(); } } //Блок кода асинхронной загрузки данных из БД @Override public Loader<Cursor> onCreateLoader(int id, Bundle bnd) { return new MyLoader(this, db, id); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { switch (loader.getId()) { case 1: getSupportLoaderManager().getLoader(1).forceLoad(); if (cursor.moveToLast()) { num1 = cursor.getFloat(0); } else { num1 = 0; } String n1 = new DecimalFormat("#0.0").format(num1); num_1.setText(n1); break; } } @Override public void onLoaderReset(Loader<Cursor> loader) { } static class MyLoader extends CursorLoader { DataBase db; Cursor cursor; final int LoaderID; public MyLoader (Context context, DataBase db, int id) { super(context); this.db = db; LoaderID = id; } @Override public Cursor loadInBackground() { switch (LoaderID) { case 1:cursor = db.last_add(); break; } return cursor; } } public void hideFab () { AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar); appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) { fab.animate() .scaleX(0f) .scaleY(0f) .start(); } else if (verticalOffset == 0) { fab.animate() .scaleX(1f) .scaleY(1f) .start(); } else { fab.animate() .scaleX(0.3f) .scaleY(0.3f) .start(); } } }); } } 

    1 answer 1

    The problem was to call getSupportLoaderManager().getLoader(1).forceLoad(); in onLoadFinished . Forceload forcedly looped the loading of data from the database, which prevented the execution of the hiding fab code. Correctly call getSupportLoaderManager().getLoader(1) , and Forceload if you need to update the data