我是这里的新成员。 我已经针对我的问题进行了两天的搜索,但是我没有找到任何东西。 所以我邀请你寻求帮助。
我有一个导航抽屉这是一个我希望放置片段的类别,当我向左或向右滑动时,我的日历会显示不同的日期。 所以我在一个片段中开发了一个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>
如何在主要活动中显示此页面查看器? 谢谢你的阅读
答案 0 :(得分:0)
在您TestSlideFragment
onActivityCreated
替换下面的代码
ViewPager = new ViewPager(mContext);
ViewPager.setId(R.id.slide);
mContext.setContentView(ViewPager);
带
ViewPager = (ViewPager)getView().findViewById(R.id.page);
尝试再次运行您的应用程序。
答案 1 :(得分:0)
为什么要在setcontentview方法中添加viewpager?这为整个活动设置了viewpager。
在片段中找到您的viewpager,如
vp = (ViewPager) view.findViewById(R.id.viewpager).