0

我正在使用 java 在 android studio 中制作应用程序。该应用程序由一个导航抽屉和一个底部导航组成,目前我正在使用 2 个包含我想要显示的元素的片段。在选项卡之间切换以及在导航抽屉中选择另一个选项时,两个片段的元素会重叠。

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle actionBarDrawerToggle;
    Toolbar toolbar;
    NavigationView navigationView;
    //variables para cargar el fragment
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    String personName;
    ViewPager2 viewPager2;
    BottomNavigationView  bottomNavigationView;
    ViewPagerAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);//se pasa el toolbar
        drawerLayout=findViewById(R.id.drawer);
        navigationView=findViewById(R.id.navigationView);
        navigationView.setNavigationItemSelectedListener(this);
        actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
        actionBarDrawerToggle.syncState();
        //cargar fragment principal
        FragmentManager fragmentManager;
        FragmentTransaction fragmentTransaction;
       /* fragmentManager=getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.container, new perfil_user());
        fragmentTransaction.commit();*/
        initView();



    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.user){
            FragmentManager fragmentManager;
            FragmentTransaction fragmentTransaction;
            fragmentManager=getSupportFragmentManager();
            fragmentTransaction=fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, new perfil_user());
            fragmentTransaction.commit();

        }
        else if (item.getItemId() == R.id.exit){
            finish();

        }
        //aquí van las otros fragment
        drawerLayout.closeDrawer(GravityCompat.START);
        return false;
    }
    private void initView() {
        viewPager2 = findViewById(R.id.vp_horizontal_ntb);
        viewPager2.setOffscreenPageLimit(100);
        bottomNavigationView = findViewById(R.id.navigation);
        adapter = new ViewPagerAdapter(this);
        viewPager2.setAdapter(adapter);


       bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
           @Override
           public boolean onNavigationItemSelected(@NonNull MenuItem item) {
               Fragment fragment=null;
               switch (item.getItemId()){
                   case R.id.perfil:
                       fragment=new perfil_user();
                       break;
                   case R.id.generar_debate:
                       fragment=new genera_debate();
                       break;

               }
               getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment).commit();
               return true;
           }
       });



    }
}
4

1 回答 1

0

我建议你看看这个答案

https://stackoverflow.com/a/18413437/8195076

于 2021-08-02T07:32:40.927 回答