android 如何合并底部导航视图和视图分页器?

7dl7o3gd  于 2022-12-21  发布在  Android
关注(0)|答案(3)|浏览(141)

我需要帮助来组合ViewPager和BottomNavigationView。但是当我在ViewPager工作时滑动屏幕时,碎片与另一个碎片堆叠在一起。我该如何修复此问题?

    • 堆叠碎片**

    • 在正常片段上滑动之前**

下面是我的代码:

    • 主要活动. 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);
}
    • 活动_主要. 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>
    • 查看页面适配器. 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;
}

先谢了!

ccgok5k5

ccgok5k51#

在主活动中更改此代码

@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;
}

为选择底部导航选项卡添加此代码。

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) {

            }
        });
kmpatx3s

kmpatx3s2#

作为@Chintan先生的回答,我替换为:

return new FooFragment();

viewPager.setCurrentItem(index);

并删除**loadFragment()**函数,它现在可以完美地工作了。非常感谢!

wnvonmuf

wnvonmuf3#

binding.bottomNavigation.setOnClickMenuListener(model -> {
        switch (model.getId()) {
            case 1:
                binding.viewPager.setCurrentItem(0);
                break;
            case 2:
                binding.viewPager.setCurrentItem(1);
                break;
            case 3:
                binding.viewPager.setCurrentItem(2);
                break;
            case 4:
                binding.viewPager.setCurrentItem(3);
                break;
        }
        return null;
    });

相关问题