我在MainActivity中创建了一个BottomNavigationView(以及最新的JetPack导航组件),我需要根据用户的身份验证状态在不同的Fragment中显示/隐藏它。
就在几天前,我还只是使用requireActivity().findViewById(R.id.nav_view)从Fragment中检索它(我意识到这不是最佳实践,但目前还可以接受)。
在升级到Android Studio Bumblebee沿着其他一些导航库后,会出现NullPointerException。
然后我尝试使用下面的界面,但仍然得到NPE。
我真的希望仍然创建底部酒吧 * 在一个地方只 *,并能够检索它从任何地方相应。
main_activity.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements HomeFragment.onGetBottomNavViewListener {
private ActivityMainBinding binding;
private BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
bottomNavigationView = binding.navView;
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
@Override
public void showBottomNavView() {
bottomNavigationView.setVisibility(View.VISIBLE);
}
@Override
public void hideBottomNavView() {
bottomNavigationView.setVisibility(View.INVISIBLE);
}
}
HomeFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.home.HomeFragment"
android:paddingStart="@dimen/activity_padding"
android:paddingTop="@dimen/activity_padding"
android:paddingEnd="@dimen/activity_padding"
android:paddingBottom="100dp">
<LinearLayout>....</LinearLayout>
<GridLayout>....</GridLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/search_btn"
style="@style/BaseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginBottom="5dp"
android:text="Go!"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/gridLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
HomeFragment.java
public class HomeFragment extends Fragment implements View.OnClickListener {
private onGetBottomNavViewListener listener;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
user = FirebaseAuth.getInstance().getCurrentUser();
View root = binding.getRoot();
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((AppCompatActivity) requireActivity()).getSupportActionBar().setSubtitle("");
// If user is logged in
if (user != null) {
listener.showBottomNavView();
} else {
listener.hideBottomNavView();
}
}
// INTERFACE
public interface onGetBottomNavViewListener {
void showBottomNavView();
void hideBottomNavView();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
// Store the listener (activity) that will have events fired once the fragment is attached
if (context instanceof onGetBottomNavViewListener) {
listener = (onGetBottomNavViewListener) context;
} else {
throw new ClassCastException(context +
"must implement HomeFragment.onGetBottomNavViewListener");
}
}
}
UPDATE:仍然无法从片段中获取bottomBar,但我通过简单地在MainActivity中移动身份验证检查沿着bar管理来解决这个特殊情况,方法是使用
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
navView.setVisibility(View.GONE);
} else {
navView.setVisibility(View.VISIBLE);
}
}
});
1条答案
按热度按时间hrysbysz1#
您可以创建一个方法来显示和隐藏Activity中的BottomNavugationView。
或