フラグメントからToolBarのアイテムを制御できるようにした

This commit is contained in:
ろむねこ 2024-01-22 11:26:11 +09:00
parent d6f4885b4a
commit 05997eafe1
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168
2 changed files with 44 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package one.nem.lacerta; package one.nem.lacerta;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController; import androidx.navigation.NavController;
@ -17,6 +18,7 @@ import android.widget.Toast;
import one.nem.lacerta.model.pref.FeatureSwitchOverride; import one.nem.lacerta.model.pref.FeatureSwitchOverride;
import one.nem.lacerta.utils.FeatureSwitch; import one.nem.lacerta.utils.FeatureSwitch;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
@ -68,6 +70,40 @@ public class MainActivity extends AppCompatActivity {
// Set status bar color // Set status bar color
getWindow().setStatusBarColor(ContextCompat.getColor(this, one.nem.lacerta.shared.ui.R.color.colorSurface)); getWindow().setStatusBarColor(ContextCompat.getColor(this, one.nem.lacerta.shared.ui.R.color.colorSurface));
// Set app bar color
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
// Collapsed
getWindow().setStatusBarColor(ContextCompat.getColor(getApplicationContext(), one.nem.lacerta.shared.ui.R.color.colorSecondaryContainer));
} else if (verticalOffset == 0) {
// Expanded
getWindow().setStatusBarColor(ContextCompat.getColor(getApplicationContext(), one.nem.lacerta.shared.ui.R.color.colorSurface));
} else {
// Somewhere in between
// Here you can add a color transition if you want
}
}
});
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
// Public
public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
public void setActionBarBackButton(boolean isEnabled) {
getSupportActionBar().setDisplayHomeAsUpEnabled(isEnabled);
}
public void setActionBarMenuItem(int itemId, boolean isEnabled) {
} }
private void initializeApp() { private void initializeApp() {

View File

@ -10,6 +10,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
@ -70,6 +71,7 @@ public class HomeTopFragment extends Fragment {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home_top, container, false); View view = inflater.inflate(R.layout.fragment_home_top, container, false);
setHasOptionsMenu(true);
return view; return view;
} }
@ -96,6 +98,12 @@ public class HomeTopFragment extends Fragment {
}); });
}); });
} }
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.drawer_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
} }