Abajo tenéis mi fragment_home.xml y HomeFragment.xml. Si hace falta, puedo subir y el MainActivity.java también.
MainActivity.java
package com.e.events;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import com.e.events.Fragment.HomeFragment;
import com.e.events.Fragment.NotificationsFragment;
import com.e.events.Fragment.ProfileFragment;
import com.e.events.Fragment.SaveFragment;
import com.e.events.Fragment.SearchFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity implements DrawerLocker, NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
BottomNavigationView bottomNavigationView;
Fragment selectedFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
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 (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.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;
}
drawer.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:
selectedFragment = new HomeFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
case R.id.nav_notifications:
selectedFragment = 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();
selectedFragment = new ProfileFragment();
break;
case R.id.nav_save:
selectedFragment = new SaveFragment();
break;
}
if (selectedFragment != null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
}
return true;
}
};
public void setDrawerLocked(boolean enabled){
if(enabled){
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}else{
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
}