[英]ViewPager not showing second fragment
我試圖在一個 ViewPager 中顯示 4 個片段,但是當我在片段之間滾動時,第二個片段根本沒有顯示。 它僅正確顯示片段 1,3 和 4。
我已經檢查了很多關於此類內容的其他答案,他們建議使用
getChildFragmentManager()
代替
getSupportFragmentManager()
並且似乎可以工作,但是我不能使用它,因為我正在使用 androidx。
這是我在 MainActivity class 中的代碼:
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 適配器 class:
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 會正確顯示所有片段!
知道我可能做錯了什么或我錯過了什么嗎?
提前致謝!
在您的 SectionPagerAdapter class 在 case 語句中添加 break 並嘗試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.