ViewPager 不调用 onCreate 和 onCreateView 片段方法

ViewPager doesn't call onCreate and onCreateView fragment methods

我创建了一个带有标签的 activity。但是,未调用片段 onCreateonCreateView

这是我的片段 class:

public static class PlaceholderFragment extends Fragment {
    private int mPage;
    private static final String ARG_PAGE = "section_number";

    public PlaceholderFragment() {
    }
    public static PlaceholderFragment newInstance(int sectionNumber) {
        Log.d(TAG, "newInstance: run");
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: run");
        if (getArguments() != null) {
            mPage = getArguments().getInt(ARG_PAGE);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: run");
        View rootView = inflater.inflate(R.layout.fragment_easy, container, false);
        RecyclerView recyclerView = rootView.findViewById(R.id.recyclerView2);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        MyAdapter adapter = new MyAdapter(getContext(), initData(mPage));
        adapter.setParentClickableViewAnimationDefaultDuration();
        adapter.setParentAndIconExpandOnClick(true);
        recyclerView.setAdapter(adapter);
        return rootView;
    }
}

activity 的 OnCreate 方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_easy);
    mSectionsPagerAdapter = new SectionsPagerAdapter(new FragmentActivity().getSupportFragmentManager());
    // Set up the ViewPager with the sections adapter.
    mViewPager = findViewById(R.id.viewpager);
    mTabLayout = findViewById(R.id.tabs2);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mTabLayout.setupWithViewPager(mViewPager);
}

片段XML:

<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.stunner.moderstars.ActivityEasy$PlaceholderFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

Activity XML:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/easy_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:layout_scrollFlags="scroll|enterAlways|snap"
            ads:menu="@menu/menu_activity_pro"
            ads:title="@string/app_name">

        </androidx.appcompat.widget.Toolbar>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:tabMode="scrollable" />
    </com.google.android.material.appbar.AppBarLayout>

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

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="32dp"
        android:src="@drawable/ic_sharp_add"
        ads:backgroundTint="@color/AccentColor" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

适配器Class:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return UsefulThings.checkmods(getApplicationContext()).length+1;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "Mod #"+ (position+1);
    }
}

我在某处读到,它可能是布局问题,但我不知道在那里更改什么。

已解决。问题出在这里:

mSectionsPagerAdapter = new SectionsPagerAdapter(new FragmentActivity().getSupportFragmentManager());

我已更正为:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());