在我的 mainActivity 的片段中显示 ViewPager

Display ViewPager in fragment of my mainActivity

我是这里的新会员。 我已经对我的问题进行了两天的多次搜索,但我没有找到任何东西。 所以我邀请你寻求帮助。

我有一个导航抽屉 这是一个类别,我想放置一个片段,当我向左或向右滑动时,我的日历会显示不同的日期。 所以我在片段中开发了一个ViewPager。这个片段是我的导航抽屉的片段。 我的问题:ViewPage 没有出现。

显示的要么是页面查看器,要么是空白片段。 这是改变显示的代码行:

mContext.setContentView(ViewPager);

screen without this code screen with this code 数字“89”是一个随机数,滑动时递增 1。

不同部分代码: 在主要活动中显示片段:

TestSlideFragment fragment = new TestSlideFragment();
fragmentTransaction.replace(R.id.RelativeLayout_for_Fragment, fragment, fragment.getTag());
fragmentTransaction.commit();

testSlideFragment(java 和 xml)

public class TestSlideFragment extends Fragment {
    View view = null;
    private static final int NUM_PAGES = 5;
    int nbr = 0 ;

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    ViewPager ViewPager;
    FragmentStatePagerAdapter mAdapt;
    //private TabsAdapter d;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;


    public TestSlideFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment



         view = inflater.inflate(R.layout.fragment_test_slide, container, false);
        return  view;
    }

    /**
     * The number of pages (wizard steps) to show in this demo.
     */

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Activity mActivity =  getActivity();
        FragmentActivity mContext;
        mContext=(FragmentActivity) mActivity;

        ViewPager = new ViewPager(mContext);
        ViewPager.setId(R.id.slide);
        mContext.setContentView(ViewPager);

        mPagerAdapter = new ScreenSlidePagerAdapter(mContext.getSupportFragmentManager());
        ViewPager.setAdapter(mPagerAdapter);
        ViewPager.setCurrentItem(1);

    }



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

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

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


}

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:tag="fragment_test_slide"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="oytoch.iut_info.test.TestSlideFragment">

    <!-- TODO: Update blank fragment layout -->
    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/page"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

ScreenSlidePageFragment

public class ScreenSlidePageFragment extends Fragment {
ViewGroup rootView = null;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
         rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_screen_slide_page,container,false);


    return rootView;
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Activity mActivity = getActivity();
    Context mContext = getActivity();

    TextView slide_txt = (TextView) rootView.findViewById(R.id.slide);

    SharedPreferences prefs = mContext.getSharedPreferences(MyPREFERENCES , MODE_PRIVATE);
    int restoredText = prefs.getInt("nbr", 0);

    slide_txt.setText(Integer.toString(restoredText));

    sharedpreferences = mContext.getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt("nbr", 1+ restoredText);
    editor.commit();
}

}

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">


    <ScrollView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView`enter code here`
            android:padding="16dp"
            android:id="@+id/slide"
            android:lineSpacingMultiplier="1.2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="lorem_ipsum" />
    </ScrollView>
</FrameLayout>

如何在主 activity 中显示此页面查看器? 感谢阅读我

在您的 TestSlideFragment 中替换下面您在 onActivityCreated

中编写的代码
ViewPager = new ViewPager(mContext);
    ViewPager.setId(R.id.slide);
    mContext.setContentView(ViewPager);

ViewPager = (ViewPager)getView().findViewById(R.id.page);

并再次尝试 运行 您的应用程序。

为什么要在 setcontentview 方法中添加 viewpager?这为整个 activity 设置了 viewpager。

这样的片段中找到您的 viewpager
vp = (ViewPager) view.findViewById(R.id.viewpager).