ViewPager не вызывает методы фрагментов onCreate и onCreateView - PullRequest
2 голосов
/ 03 февраля 2020

Я создал активность с вкладками. Однако фрагменты onCreate и onCreateView не вызываются.

Вот мой класс Fragment:

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

Метод действия 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>

Активность 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>

Класс адаптера:

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

Я где-то читал, что это может быть проблема макета, но я не знаю, что там изменить.

1 Ответ

0 голосов
/ 03 февраля 2020

решаемая. Проблема была здесь:

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

Я исправил это до:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

...