我是Android开发的新手,似乎没有其他人遇到这个问题,所以也许我只是密集了。
无论如何,我的问题是我的格式化viewPager每次更改屏幕时都会显示相同的字符串。我使用this教程来创建它,因此我的代码与示例代码相同。如何正确格式化代码/我应该添加什么代码以便在翻页时显示不同的屏幕?
编辑:我的代码
主
public class Home extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePager.ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
}
else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
public 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;
}
}
ScreenSlidePageFragment
public class ScreenSlidePageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup)
inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
}
fragment_screen_slide_page
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView style="?android:textAppearanceMedium"
android:padding="16dp"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum" />
Home.xml
<LinearLayout
android:layout_width="390dp"
android:layout_height="200dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/ffweButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/homeButton"
app:layout_constraintVertical_bias="0.6">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
答案 0 :(得分:1)
尝试像这样添加片段到您的适配器
adapter.addFragment(new Fragment1(), "FRAG1");
adapter.addFragment(new Fragment2(), "FRAG2");
adapter.addFragment(new Fragment3(), "FRAG3");
然后调用它
viewPager.setAdapter(adapter);
你还可以发布活动home.xml吗?