ViewPager不会调用onCreate和onCreateView片段方法

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

我创建了带有标签的活动。但是,片段onCreateonCreateView没有被调用。

这是我的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);
    }
}

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

java android android-tabs
1个回答
0
投票

已解决。问题出在这里:

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

我已将其更正为:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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