Fragments controlled by a ViewPager in a Fragment do not appear

The top activity in my application contains a fixed tab bar with a custom ViewPager (disabled swipe gestures) and a FragmentPagerAdapter.

This tab bar gives access to three different Fragments and in the first Fragment I want to be able to swipe between views relevant to that Fragment.

Initially I tried to use a ViewFlipper but I couldn't get the same gestures that you get with a ViewPager so now I'm trying to implement the ViewPager in the fragment.

The code I'm using for the ViewPager is from the AnimationsDemo project which can be found on this page about the ViewPager. Though I did make some small adjustments by removing the lower bar which allows you to switch between views using button presses.

This is what my Fragment looks like:

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;    

public class NewsFragment extends Fragment {
    private static final int NUM_PAGES = 5;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;    

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

        mPager = (ViewPager) rootView.findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
        new setAdapterTask().execute();

        return rootView;
    }

    private class setAdapterTask extends AsyncTask<Void,Void,Void>{
          protected Void doInBackground(Void... params) {
                return null;
            }    

            @Override
            protected void onPostExecute(Void result) {
                       mPager.setAdapter(mPagerAdapter);
            }
    }

    /**
     * A simple pager adapter that represents 5 {@link ScreenSlidePageFragment} objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }    

        @Override
        public Fragment getItem(int position) {
            return ScreenSlidePageFragment.create(position);
        }    

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

And the XML that belongs to it:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/textviewscreenslide"
        android:text="Screen slide fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        />
    <android.support.v4.view.ViewPager
        android:layout_below="@+id/textviewscreenslide"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

I'm using an AsyncTask due to this post here on StackOverflow, because I occasionally got that error when quickly switching between tab items.

This is what the Fragment that I want to be displayed looks like:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;    

public class ScreenSlidePageFragment extends Fragment {
    public static final String ARG_PAGE = "page";
    private int mPageNumber;    

    public static ScreenSlidePageFragment create(int pageNumber) {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);
        fragment.setArguments(args);
        return fragment;
    }    

    public ScreenSlidePageFragment() {
    }    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPageNumber = getArguments().getInt(ARG_PAGE);
    }    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.title_template_step, mPageNumber + 1));
        return rootView;
    }    

    public int getPageNumber() {
        return mPageNumber;
    }
}

And the XML that goes with it:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">
        <TextView android:id="@android:id/text1"
            style="?android:textAppearanceLarge"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp" />
        <TextView style="?android:textAppearanceMedium"
            android:lineSpacingMultiplier="1.2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum" />
    </LinearLayout>
</ScrollView>

So when I run the program the Fragment containing the ViewPager appears just fine, I can see the text "Screen slide fragment" at the top and I don't get any errors. I can actually also swipe the ViewPager in this fragment. When I swipe to the right I can see the blue indicator appearing on the left that I'm at the first Fragment and when I swipe to the left a few times this indicator appears on the right side. So the ViewPager is actually there, the Fragments just do not appear.

I came across this project, which is about a nested ViewPager, but it appears that this one requires at least API level 17. So it might not even be possible.

So my question is, is there a way to make these Fragments appear (only supporting 4.0 and newer) or is there another more efficient way to be able to switch between fragments/views (all the views for the fragments in this pager will be exactly the same in terms of layout, only difference is the text in the TextViews). Maybe there's even another thread exactly explaining the same issue which I've completely missed.


Fixing this was easy. I used the code from this project together with the v4 support library. The issue I had why it didn't work was because I also had the v13 support library in the project which interfered with getChildFragmentManager() . Due to v13 being there that function was nog recognised, even though I never actually used v13.

链接地址: http://www.djcxy.com/p/68144.html

上一篇: 网站截图

下一篇: 片段中由ViewPager控制的片段不会显示