Код для класса, в котором я объявил нижнюю панель приложения:
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class NavigActivity extends AppCompatActivity {
private BottomAppBar mbottomAppBar;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navig);
mbottomAppBar = findViewById(R.id.bottom_app_bar);
setSupportActionBar(mbottomAppBar);
fab= findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new AddNewProductFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
});
mbottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Fragment fragment = null;
switch (item.getItemId())
{
case R.id.home:
fragment = new HomeFragment();
break;
case R.id.notification:
fragment = new NotificationFragment();
break;
case R.id.my_invoices:
fragment = new InvoiceFragment();
break;
}
if(fragment != null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
return false;
}
});
mbottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//open bottom sheet
BottomSheetDialogFragment bottomSheetDialogFragment = BottomSheetNavigationFragment.newInstance();
bottomSheetDialogFragment.show(getSupportFragmentManager(), "Bottom Sheet Dialog Fragment");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
}
Код для фрагмента навигации нижнего листа:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.navigation.NavigationView;
public class BottomSheetNavigationFragment extends BottomSheetDialogFragment {
public static BottomSheetNavigationFragment newInstance() {
Bundle args = new Bundle();
BottomSheetNavigationFragment fragment = new BottomSheetNavigationFragment();
fragment.setArguments(args);
return fragment;
}
//Bottom Sheet Callback
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//check the slide offset and change the visibility of close button
if (slideOffset > 0.5) {
closeButton.setVisibility(View.VISIBLE);
} else {
closeButton.setVisibility(View.GONE);
}
}
};
private ImageView closeButton;
@SuppressLint("RestrictedApi")
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
//Get the content View
View contentView = View.inflate(getContext(), R.layout.bottom_navigation_drawer, null);
dialog.setContentView(contentView);
NavigationView navigationView = contentView.findViewById(R.id.navigation_view);
//implement navigation menu item click event
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
F
Fragment fragment;
switch (item.getItemId()) {
case R.id.user_details:
break;
case R.id.settings:
break;
case R.id.help:
break;
case R.id.logout:
MainActivity obj = new MainActivity();
obj.logout();
startActivity( new Intent(getActivity(), MainActivity.class));
break;
}
return false;
}
});
closeButton = contentView.findViewById(R.id.close_image_view);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//dismiss bottom sheet
dismiss();
}
});
//Set the coordinator layout behavior
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
//Set callback
if (behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
}
}
Я не могу понять как мы можем переместить экран из этого в другой фрагмент - как getSupportFragmentManager () не доступен в этом. Когда я пытаюсь сгенерировать намерение, оно выдает ошибку.
Любой совет или помощь приветствуются. Спасибо!