BottomNavigationView не работает после добавления TabLayout и Viewpager
Почему мой BottomNaigationView не работает с моим TabLayout и Viewpager. До этого, BottomNavigationView работал отлично. Я смог переключиться между фрагментами, назначенными каждому элементу в BottomNavigationView, но теперь после добавления TabLayout и Viewpager, не позволяет мне изменять между фрагментами в моем BottomNavigationView.
Может быть, это что-то не так с моим main_activity.xml файл
MainActivity
public class MainActivity extends AppCompatActivity implements DrawerLocker, NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawer;
ImageView mImageProfile;
TextView mFullName;
BottomNavigationView mBottomNavigationView;
Fragment mSelectedFragment = null;
FirebaseUser mFirebaseUser;
String mProfileID;
ViewPager mViewPager;
ViewPagerAdapter mViewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
ImageView options = findViewById(R.id.options);
options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDrawer.openDrawer(GravityCompat.START);
}
});
ImageView camera = findViewById(R.id.camera);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, PostActivity.class);
startActivity(intent);
}
});
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawer = findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawer.addDrawerListener(toggle);
toggle.syncState();
TabLayout tabLayout = findViewById(R.id.tabLayout);
mViewPager = findViewById(R.id.viewPager);
mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mViewPagerAdapter.addFragment(new HomeFragment(), "Home");
mViewPagerAdapter.addFragment(new WorldFragment(), "World");
mViewPagerAdapter.addFragment(new MessagesFragment(), "Messages");
mViewPager.setAdapter(mViewPagerAdapter);
tabLayout.setupWithViewPager(mViewPager);
NavigationView navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
mBottomNavigationView = findViewById(R.id.bottomNavigation);
mBottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
View headerLayout = navigationView.getHeaderView(0);
mImageProfile = headerLayout.findViewById(R.id.image_profile);
mFullName = headerLayout.findViewById(R.id.headerFullName);
SharedPreferences prefs = getApplicationContext().getSharedPreferences("PREFS", Context.MODE_PRIVATE);
mProfileID = prefs.getString("profileid", "none");
userInfo();
Bundle intent = getIntent().getExtras();
if (intent != null) {
String publisher = intent.getString("publisherid");
SharedPreferences.Editor editor = getSharedPreferences("PREFS", MODE_PRIVATE).edit();
editor.putString("profileid", publisher);
editor.apply();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
} else {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
}
@Override
public void onBackPressed() {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
mDrawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_edit_profile:
Intent editProfile = new Intent(MainActivity.this, EditProfileActivity.class);
startActivity(editProfile);
break;
case R.id.nav_settings:
Intent settings = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(settings);
break;
case R.id.nav_logout:
Intent logout = new Intent(MainActivity.this, StartActivity.class);
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, StartActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
break;
}
mDrawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_edit_profile) {
return true;
}
return super.onOptionsItemSelected(item);
}
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
mSelectedFragment = new HomeFragment();
break;
case R.id.nav_search:
mSelectedFragment = new SearchFragment();
break;
case R.id.nav_notifications:
mSelectedFragment = new NotificationsFragment();
break;
case R.id.nav_profile:
SharedPreferences.Editor editor = getSharedPreferences("PREFS", MODE_PRIVATE).edit();
editor.putString("profileid", FirebaseAuth.getInstance().getCurrentUser().getUid());
editor.apply();
mSelectedFragment = new ProfileFragment();
break;
case R.id.nav_save:
mSelectedFragment = new SaveFragment();
break;
}
if (mSelectedFragment != null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mSelectedFragment).commit();
}
return true;
}
};
public void setDrawerLocked(boolean enabled) {
if (enabled) {
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
} else {
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
private void userInfo() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(mFirebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (getApplicationContext() == null) {
return;
}
User user = dataSnapshot.getValue(User.class);
if (user != null) {
mFullName.setText(user.getFullname());
Glide.with(getApplicationContext()).load(user.getImageurl()).into(mImageProfile);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
static class ViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> mFragments;
private ArrayList<String> mTitles;
ViewPagerAdapter(FragmentManager fm) {
super(fm);
this.mFragments = new ArrayList<>();
this.mTitles = new ArrayList<>();
}
@NonNull
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mTitles.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
}
}
activity_main.xml
<androidx.drawerlayout.widget.DrawerLayout 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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Activity.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_home_fragment"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?android:attr/windowBackground"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/events_logo_main_activity"
android:layout_width="180dp"
android:layout_height="45dp"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:contentDescription="@string/logo"
android:src="@drawable/events_logo_black_max_size" />
<ImageView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_marginEnd="11dp"
android:contentDescription="@string/camera"
android:src="@drawable/ic_camera_create_events_home_fragment_black" />
<ImageView
android:id="@+id/options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:contentDescription="@string/options"
android:src="@drawable/ic_three_bars_settings_home_fragment_black" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?android:attr/windowBackground"
android:elevation="4dp"
app:tabIndicatorColor="@color/colorPrimaryAqua50"
app:tabSelectedTextColor="@color/colorPrimaryAqua50"
app:tabTextColor="@color/colorPrimaryDark" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom" />
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:elevation="4dp"
app:itemIconTint="@color/selector"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_navigation_bar" />
</com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:itemTextColor="#00ffff"
app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>