我已经设置了一个BottomAppBar
,NavigationDrawer
但我无法恢复在此抽屉中选择的项目,这是一个片段。
我遵循了许多教程来设置它,BottomAppBar
但我从来没有看到如何从抽屉中获取选择。
这是我的抽屉是如何创建的:
public class MaintainerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maintainer);
setUpBottomAppBar();
}
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.main_container, fragment);
transaction.commit();
}
private void setUpBottomAppBar() {
BottomAppBar bottomAppBar = findViewById(R.id.bottom_app_bar);
setSupportActionBar(bottomAppBar);
bottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BottomSheetDialogFragment bottomSheetDialogFragment = BottomSheetNavigationMaintainerFragment.newInstance();
bottomSheetDialogFragment.show(getSupportFragmentManager(), "Bottom Sheet Dialog Fragment");
Bundle args = bottomSheetDialogFragment.getArguments();
}
});
bottomAppBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
最后,这是我的片段的代码:
public class BottomSheetNavigationMaintainerFragment extends BottomSheetDialogFragment {
public BottomSheetNavigationMaintainerFragment() {
// Required empty public constructor
}
public static BottomSheetNavigationMaintainerFragment newInstance() {
Bundle args = new Bundle();
BottomSheetNavigationMaintainerFragment fragment = new BottomSheetNavigationMaintainerFragment();
fragment.setArguments(args);
return fragment;
}
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) { }
};
@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.fragment_bottom_navigation_maintainer, 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) {
switch (item.getItemId()) {
case R.id.app_bar_assets:
break;
case R.id.app_bar_items:
break;
}
return false;
}
});
ImageView 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);
}
}
}
因此,我想在onNavigationItemSelected
方法中返回所选项目,但我找不到将此片段链接到创建它的活动的方法