片段内的最后一个选项卡式片段未显示

时间:2019-09-16 15:40:33

标签: android android-fragments android-viewpager android-tablayout

我有一个具有TabLayout和ViewPager的父片段。我在TabLayout中有三个选项卡(每个选项卡一个片段)。现在,前两个选项卡可以正确显示,但最后一个选项卡将不会显示,或者在某些情况下,它会显示并立即消失。任何帮助表示赞赏。

我已经阅读了有关此问题的几篇文章,但无法解决任何问题。 1. Fragment Tabs inside Fragment 2. Tab Fragments not showing 3. Viewpager fragments not shown after the first time 4. https://github.com/codepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter

我现在很绝望。

HomeFragment:

public class HomeFragment extends Fragment {

    TabLayout mTabLayout;
    ViewPager mViewPager;
    EventPagerAdapter mEventPagerAdapter;

    public HomeFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        mTabLayout = view.findViewById(R.id.tabs);
        mViewPager = view.findViewById(R.id.view_pager);

        mEventPagerAdapter = new EventPagerAdapter(getChildFragmentManager(), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mEventPagerAdapter.add(new AFragment());
        mEventPagerAdapter.add(new BFragment());
        mEventPagerAdapter.add(new CFragment());

        mViewPager.setAdapter(mEventPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        return view;
    }
}

HomeFragment xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <WrapContentViewPager
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:overScrollMode="never"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tabs" />

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tab_a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/a" />

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tab_b"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/b" />

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tab_c"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/c" />

            </com.google.android.material.tabs.TabLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>
</ScrollView>

自定义FragmentPagerAdapter:

public class EventPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> mFragments;

    public EventPagerAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);

        mFragments = new ArrayList<>();
    }

    public void add(Fragment fragment) {
        this.mFragments.add(fragment);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = mFragments.get(position);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return "A";
    }
}

AFragment / BFragment / CFragment:

public class AFragment extends Fragment {

    public AFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container, false);

        return view;
    }
}

0 个答案:

没有答案