Я использую скрытие и показ фрагментов для изменения текущего фрагмента вместо замены. но главная проблема заключается в том, что фрагмент изменяет свои свойства, поскольку вместо экрана отображается над нижней панелью навигации. HELP PLS!
Основной вид деятельности
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.view.MenuItem;
import ru.mpei.vmss.myapplication.R;
import ru.mpei.vmss.myapplication.fragments.Dashboard;
import ru.mpei.vmss.myapplication.fragments.News;
import ru.mpei.vmss.myapplication.fragments.Others;
import ru.mpei.vmss.myapplication.fragments.Profile;
import ru.mpei.vmss.myapplication.fragments.Tasks;
public class MainActivity extends AppCompatActivity {
private Dashboard dashboardFragment;
private News newsFragment;
private Profile profileFragment;
private Tasks tasksFragment;
private Others othersFragment;
private Fragment current;
FragmentManager supportFragmentManger;
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
dashboardFragment = new Dashboard();
newsFragment = new News();
profileFragment = new Profile();
tasksFragment = new Tasks();
othersFragment = new Others();
supportFragmentManger = getSupportFragmentManager();
supportFragmentManger.beginTransaction().add(R.id.activity_main, dashboardFragment).commit();
supportFragmentManger.beginTransaction().add(R.id.activity_main, newsFragment).commit();
supportFragmentManger.beginTransaction().add(R.id.activity_main, profileFragment).commit();
supportFragmentManger.beginTransaction().add(R.id.activity_main, tasksFragment).commit();
supportFragmentManger.beginTransaction().add(R.id.activity_main, othersFragment).commit();
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelected);
current = dashboardFragment;
supportFragmentManger.beginTransaction().hide(newsFragment).commit();
supportFragmentManger.beginTransaction().hide(profileFragment).commit();
supportFragmentManger.beginTransaction().hide(tasksFragment).commit();
supportFragmentManger.beginTransaction().hide(othersFragment).commit();
supportFragmentManger.beginTransaction().show(dashboardFragment).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelected = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch(menuItem.getItemId()){
case R.id.navigation_dashboard:
supportFragmentManger.beginTransaction().hide(current).show(dashboardFragment).commit();
current = dashboardFragment;
return true;
case R.id.navigation_news:
supportFragmentManger.beginTransaction().hide(current).show(newsFragment).commit();
current = newsFragment;
return true;
case R.id.navigation_profile:
supportFragmentManger.beginTransaction().hide(current).show(profileFragment).commit();
current = profileFragment;
return true;
case R.id.navigation_tasks:
supportFragmentManger.beginTransaction().hide(current).show(tasksFragment).commit();
current = tasksFragment;
return true;
case R.id.navigation_others:
supportFragmentManger.beginTransaction().hide(current).show(othersFragment).commit();
current = othersFragment;
return true;
}
return false;
}
};
}
Основная деятельность XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
tools:context=".activities.MainActivity">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/navigation"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:itemBackground="@color/bgBottomNavigation"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation" />
</RelativeLayout>
Макет фрагмента
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.News">
<TextView
android:id="@+id/newsHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:fontFamily="@font/roboto_bold"
android:text="@string/news"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold" />
<ListView
android:id="@+id/newsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/newsHeader"/>
</RelativeLayout>