web-dev-qa-db-fra.com

Comment combiner BottomNavigationView et ViewPager?

J'ai besoin d'aide pour combiner viewpager et bottomnavigationview. Mais lorsque je balaye l'écran en fonction des travaux de visualisation, le fragment devient empilé avec un autre fragment. Comment puis-je réparer ça?

fragment empilé

1

sur un fragment normal avant glisse

1

Voici mon code:

MainActivity.java

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

private BottomNavigationView mBottomNavigation;
private ViewPager viewPager;
private ViewPagerAdapter mViewPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadFragment(new HomeFragment());
    mBottomNavigation = findViewById(R.id.buttom_navigation);
    mBottomNavigation.setOnNavigationItemSelectedListener(this);

    viewPager = findViewById(R.id.view_pager);
    mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mViewPagerAdapter);
}

private boolean loadFragment(Fragment fragment) {
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container_frame_layout, fragment);
        ft.commit();
        return true;
    }
    return false;
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    Fragment fragment = null;
    switch (menuItem.getItemId()) {
        case R.id.menu_home:
            fragment = new HomeFragment();
            break;
        case R.id.menu_favorite:
            fragment = new FavoriteFragment();
            break;
        case R.id.menu_account:
            fragment = new AccountFragment();
            break;
    }
    return loadFragment(fragment);
}

Activity_main.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"
tools:context=".MainActivity">

<FrameLayout
    Android:id="@+id/container_frame_layout"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"/>

<androidx.viewpager.widget.ViewPager
    Android:id="@+id/view_pager"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"/>

<com.google.Android.material.bottomnavigation.BottomNavigationView
    Android:id="@+id/buttom_navigation"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:background="@color/colorPrimary"
    Android:layout_alignParentBottom="true"
    app:itemIconTint="#ffff"
    app:itemTextColor="#ffff"
    app:menu="@menu/item_menu"/>
</RelativeLayout>

ViewPageradapter.java

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new HomeFragment();
        case 1:
            return new FavoriteFragment();
        case 2:
            return new AccountFragment();
    }
    return null;
}

@Override
public int getCount() {
    return 3;
}

Merci d'avance!

7
Bagus Eka Saputra

Changez ce code dans la MainActivity

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

    switch (menuItem.getItemId()) {
        case R.id.menu_home:
            viewPager.setCurrentItem(0);
            break;
        case R.id.menu_favorite:
            viewPager.setCurrentItem(1);
            break;
        case R.id.menu_account:
            viewPager.setCurrentItem(2);
            break;
    }
    return true;
}

Ajouter ce code pour sélectionner l'onglet de novigation.

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        mBottomNavigation.getMenu().findItem(R.id.menu_home).setChecked(true);
                        break;
                    case 1:
                        mBottomNavigation.getMenu().findItem(R.id.menu_favorite).setChecked(true);
                        break;
                    case 2:
                        mBottomNavigation.getMenu().findItem(R.id.menu_account).setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
2
Chintan