Я пытаюсь реализовать BottomAppBar, как в документах по Android:
Android BottomAppBar
И это самое близкое, что мне удалосьПриходите:
Я хотел бы иметь строку заголовка на ящике, как у Android, а также возможность прокрутки изакройте его с X в верхнем углу.
Вот моя текущая реализация -
frag_bottomsheet.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/activity_main_drawer">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</com.google.android.material.navigation.NavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
BottomNavigationDrawerFragment.java:
package com.jggdevelopment.wannacook.dialog_fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.jggdevelopment.wannacook.R;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class BottomNavigationDrawerFragment extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_bottomsheet, container, false);
}
}
и MainActivity.java:
package com.jggdevelopment.wannacook;
import android.content.Intent;
import androidx.fragment.app.FragmentManager;
import android.os.Bundle;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.navigation.NavigationView;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.jggdevelopment.wannacook.dialog_fragments.AddShoppingListItemDialog;
import com.jggdevelopment.wannacook.dialog_fragments.BottomNavigationDrawerFragment;
import com.jggdevelopment.wannacook.fragments.FridgeFragment;
import com.jggdevelopment.wannacook.fragments.ShoppingListFragment;
import com.jggdevelopment.wannacook.fragments.PantryFragment;
import com.jggdevelopment.wannacook.R;
/**
* Main activity for the application, handles general housekeeping and side navigation
*/
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private BottomAppBar bottomAppBar;
private FloatingActionButton mFab;
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference db = database.getReference();
private static final int RC_SIGN_IN = 123;
/**
* instantiates fields and sets the main screen to be the ShoppingListFragment
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(bottomAppBar);
mFab = findViewById(R.id.fab);
bottomAppBar = findViewById(R.id.bottom_app_bar);
ShoppingListFragment shoppingListFragment = new ShoppingListFragment();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
getSupportFragmentManager().beginTransaction()
.add(R.id.relative_layout_for_fragment, shoppingListFragment).commit();
mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
AddShoppingListItemDialog addItemDialogFragment = AddShoppingListItemDialog.newInstance("Title");
addItemDialogFragment.show(fm, "new_shopping_list_item_dialog");
}
});
bottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BottomNavigationDrawerFragment bndf = new BottomNavigationDrawerFragment();
bndf.show(getSupportFragmentManager(), bndf.getTag());
}
});
}
/**
* handles functionality for the three dots option menu in the toolbar
* @param menu menu to create
* @return true
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_drawer, menu);
return true;
}
/**
* handles what should happen when one of the options in the toolbar menu is selected
* @param item
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (item.getItemId() == R.id.action_sign_out) {
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(this, SignInActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* handles what should happen when one of the options in the navigation menu is selected
* @param item
* @return
*/
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
// home button
if (id == R.id.nav_shopping_list) {
ShoppingListFragment shoppingListFragment = new ShoppingListFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(
R.id.relative_layout_for_fragment,
shoppingListFragment,
shoppingListFragment.getTag()
).commit();
}
// fridge button
else if (id == R.id.nav_fridge) {
FridgeFragment fridgeFragment = new FridgeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(
R.id.relative_layout_for_fragment,
fridgeFragment,
fridgeFragment.getTag()
).commit();
}
// pantry button
else if (id == R.id.nav_pantry) {
PantryFragment pantryFragment = new PantryFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(
R.id.relative_layout_for_fragment,
pantryFragment,
pantryFragment.getTag()
).commit();
}
// share button
else if (id == R.id.nav_share) {
// TODO: allow user to share app link with other people
}
// contact developer button
else if (id == R.id.nav_contact) {
// TODO: send intent to email application to email developer
}
return true;
}
}