ViewPager 不显示片段

ViewPager not displaying fragments

我正在制作一个应用程序,Main Activity 有一个连接到 viewpager 的 tablayout。此 viewpager 包含三个片段。

我知道 viewpager 适配器管理 3 个片段(正确的数量),因为我使用了日志记录语句。此外,我使用 asyncTasks 将数据加载到片段中并且正确完成。加载数据时,它会填充一个 recyclerview。我使用了日志记录语句,我知道每个片段都有一个包含 20 个条目的 recyclerview,这是正确的。

但出于某种原因,我的 viewpager 不显示片段,即使当我对其调用 getItemCount() 时,我得到了正确数量的片段。

你能帮我弄清楚为什么创建了片段但没有显示吗?

谢谢。

这是我的主Activity:

public class MovieListActivity extends AppCompatActivity {

    public static final String POPULAR = "popular";
    public static final String TOP = "top";
    public static final String FAVORITES = "favorites";
    private static final String QUERY_TYPE = "query type";

    private RecyclerView mRecyclerView;
    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_list);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle(getTitle());

        setupTabLayout();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        if (findViewById(R.id.movie_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-w900dp).
            // If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;
        }
    }

    private void setupTabLayout() {
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        tabLayout.addTab(tabLayout.newTab().setText(POPULAR));
        tabLayout.addTab(tabLayout.newTab().setText(TOP));
        tabLayout.addTab(tabLayout.newTab().setText(FAVORITES));

        List<Fragment> fragments = new ArrayList<>();
        for (int i=0;i<3;i++) {
            Bundle bundle = new Bundle();
            bundle.putInt(QUERY_TYPE, i);
            fragments.add(ListFragment
                    .instantiate(this,ListFragment.class.getName(),bundle));
        }
        viewPager.setAdapter(new MoviePagerAdapter(getSupportFragmentManager(), fragments));
        tabLayout.setupWithViewPager(viewPager);
    }
}

这是viewpager应该实例化3次的片段:

public class ListFragment extends Fragment {

    private static final String QUERY_TYPE = "query type";
    public static final int POPULAR = 0;
    public static final int HIGHEST_RATED = 1;
    public static final int FAVORITES = 2;


    private RecyclerView mRecyclerView;
    private int mListType;


    public ListFragment() {}

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        mListType = this.getArguments().getInt(QUERY_TYPE);
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.movie_list,container,false);

        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.movie_list);
        assert mRecyclerView != null;
        setupRecyclerView(mRecyclerView);

        if (mListType == HIGHEST_RATED || mListType == POPULAR) {
            requestMovies();
        }

        return super.onCreateView(inflater, container, savedInstanceState);
    }


    private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
        recyclerView.setAdapter(new MovieAdapter(getActivity()));
    }

    void setMovies(Movie[] movies) {
        MovieAdapter movieAdapter = (MovieAdapter)mRecyclerView.getAdapter();
        movieAdapter.setMovies(movies);
        movieAdapter.notifyDataSetChanged();
    }

    private void requestMovies() {
        if (mRecyclerView.getAdapter().getItemCount() == 0) {
            new FetchMoviesTask(this).execute(POPULAR);
        }
    }
}

寻呼机适配器:

public class MoviePagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> mFragments;

    public MoviePagerAdapter(FragmentManager fm, List<Fragment> mFragments) {
        super(fm);
        this.mFragments = mFragments;
    }

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

    @Override
    public int getCount() {
        return mFragments==null? 0 : mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case ListFragment.POPULAR:
                return MovieListActivity.POPULAR;
            case ListFragment.HIGHEST_RATED:
                return MovieListActivity.TOP;
            case ListFragment.FAVORITES:
                return MovieListActivity.FAVORITES;
            default:
                throw new IllegalArgumentException("Requesting a fragment that doesn't exist");
        }
    }
}

以防万一:这些是 activity 和片段的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"


android:fitsSystemWindows="true"
    tools:context=".MovieListActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</android.support.v4.view.ViewPager>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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/movie_list"
    android:name="com.example.madelenko.app.moviegami.MovieListFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="GridLayoutManager"
    app:spanCount="2"
    tools:context="com.example.madelenko.app.moviegami.MovieListActivity"
    tools:listitem="@layout/movie_list_content" />

return 你在 Fragment 中膨胀的视图,

@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.movie_list,container,false);
    return rootView ;
}