【发布时间】:2022-02-03 13:15:34
【问题描述】:
我想在我的 Android 应用程序中实现 BottomNavigationView,但我的代码没有显示此 BottomNavigationView。这是我的代码。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavView;
private int startingPosition, newPosition;
private final FragmentHome fragmentHome = new FragmentHome();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
bottomNavView = findViewById(R.id.bottom_navigation);
bottomNavView.setItemIconTintList(null);
bottomNavView.setOnItemSelectedListener(item -> {
showFragment(item.getItemId());
return true;
});
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragmentHome);
bottomNavView.setSelectedItemId(R.id.action_home);
ft.commit();
}
public void showFragment(int viewId) {
Fragment fragment = null;
switch (viewId) {
case R.id.action_home:
if (bottomNavView.getSelectedItemId() != R.id.action_home) {
fragment = fragmentHome;
newPosition = 0;
}
break;
}
if (fragment != null) {
if (startingPosition > newPosition) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}
if (startingPosition < newPosition) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}
startingPosition = newPosition;
}
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_action_bar, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View arg0) {
setItemsVisibility(menu, searchItem, false);
}
@Override
public void onViewDetachedFromWindow(View arg0) {
setItemsVisibility(menu, searchItem, true);
}
});
return super.onCreateOptionsMenu(menu);
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i = 0; i < menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/menu_bottom_navigation" />
menu_bottom_navigation
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_home"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/home" />
<item
android:id="@+id/action_words_ranking"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/words_ranking" />
<item
android:id="@+id/action_users_ranking"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/users_ranking" />
<item
android:id="@+id/action_messages"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/messages" />
<item
android:id="@+id/action_notifications"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/notifications" />
</menu>
这个BottomNavigationView不仅不显示它甚至不占用任何垂直空间。
请帮帮我!
编辑
我还简化了我的日/夜主题,但没有效果。
编辑 2
我知道在哪里! ft.replace(R.id.content_frame, fragmentHome); 有问题在创建。它显示片段中的内容,但跳过 MainActivity 中的 BottomNavView 但是如何解决呢?
FragmentHome.java
public class FragmentHome extends Fragment {
private MyViewModel loginViewModel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initBinding();
loginViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String string) {
Toast.makeText(requireActivity(), "cześć " + string, Toast.LENGTH_SHORT).show();
}
});
}
private void initBinding() {
FragmentHomeBinding binding = DataBindingUtil.setContentView((requireActivity()), R.layout.fragment_home);
loginViewModel = new ViewModelProvider(this).get(MyViewModel.class);
binding.setMyViewModel(loginViewModel);
binding.setLifecycleOwner(this);
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="myViewModel"
type="pl.jawegiel.abc.viewmodel.MyViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="@{ ()-> myViewModel.setText() }"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={myViewModel.text}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
【问题讨论】:
-
这会崩溃还是不显示任何内容。因为我重新填充了您的代码,它显示了底部导航。
-
没有崩溃,只是在 Android Studio 布局预览和 pthone 中都没有显示
-
bottomNavView.setOnItemSelectedListener(item -> { showFragment(item.getItemId()); return true; }); --------------> 将此代码替换为 ::::::::> bottomNavView.setOnNavigationItemSelectedListener(item -> { showFragment(item.getItemId()); return true; }) ;
-
谢谢,但问题不在于点击项目而是显示onStart
-
你在重写 onStart() 方法吗?
标签: java android bottomnavigationview