ViewPager未显示第二个片段

问题描述 投票:1回答:1

我试图仅在一个ViewPager中显示4个片段,但是当我在片段之间滚动时,第二个片段根本不显示。它仅正确显示片段1,3和4。

我已经检查了很多其他有关此类问题的答案,他们建议使用

getChildFragmentManager() 

代替

getSupportFragmentManager()

并且似乎可以正常工作,但是我不能使用它,因为我使用的是androidx。

这是我在MainActivity类中的代码:

public class MainMenu extends AppCompatActivity {

    private ViewPager mViewPager;
    private SectionPagerAdapter sectionPagerAdapter;

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

        mViewPager = findViewById(R.id.menu_viewPager);

        sectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager(),this, getFragmentList());

        bottomNavigationView.setOnNavigationItemSelectedListener(this);

        mViewPager.setAdapter(sectionPagerAdapter);
    }

    private List<Fragment> getFragmentList() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeMenuFragment(this));
        fragmentList.add(new RestaurantsListFragment(this));
        fragmentList.add(new OrdersMenuFragment(this));
        fragmentList.add(new AccountConfigurationFragment(this));
        return fragmentList;
    }

这是我的ViewPager适配器类:

public class SectionPagerAdapter extends FragmentStatePagerAdapter {
    Context context;
    private List<Fragment> fragments = new ArrayList<>();

    public SectionPagerAdapter(FragmentManager fm, Context context, List<Fragment> fragments) {
        super(fm);
        this.context=context;
        this.fragments=fragments;
    }
    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }


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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
            case 3:
                return "SECTION 4";
        }
        return null;
    }
}

这是我的第一个片段(HomeMenuFragment)的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        >

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:fitsSystemWindows="true">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="192dp"
                android:background="@color/grayLight2"
                android:fitsSystemWindows="true"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">



                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:expandedTitleMarginEnd="64dp"
                    app:expandedTitleMarginStart="48dp"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        app:layout_collapseMode="parallax"
                        android:background="@color/grayLight2"
                        >
                        <androidx.viewpager.widget.ViewPager
                            android:id="@+id/banner_view_pager"
                            android:layout_width="match_parent"
                            android:layout_marginStart="5dp"
                            android:layout_marginEnd="5dp"
                            android:layout_marginTop="10dp"
                            android:layout_height="150dp"
                            android:overScrollMode="never"/>

                        <LinearLayout
                            android:id="@+id/slider_dots"
                            android:layout_marginTop="10dp"
                            android:layout_marginBottom="10dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:orientation="horizontal"/>

                    </LinearLayout>

                    <androidx.appcompat.widget.Toolbar
                        android:id="@+id/anim_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:layout_collapseMode="pin"
                        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                    </androidx.appcompat.widget.Toolbar>

                </com.google.android.material.appbar.CollapsingToolbarLayout>

            </com.google.android.material.appbar.AppBarLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/food_recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                />


        </androidx.coordinatorlayout.widget.CoordinatorLayout>

    </LinearLayout>

</LinearLayout>

我意识到导致此问题的原因是我在第一个Fragment的XML文件中所包含的内容:

CoordinatorLayout
AppBarLayout
CollapsingToolbarLayout
Toolbar

如果删除此元素,则ViewPager会正确显示所有片段!

关于我可能做错了什么或我想念什么的任何想法?

提前感谢!

java android android-fragments android-viewpager
1个回答
0
投票

在您的SectionPagerAdapter类中,在case语句中添加break并尝试

© www.soinside.com 2019 - 2024. All rights reserved.