Mairead
Mairead

Reputation: 275

viewPager showing the same page every time

I'm new to android development and no one else seems to be having this problem, so maybe I'm just dense.

Anyway, my issue is that my formatted viewPager shows the same string every time I change the screen. I used this tutorial to create it, so my code is the same as that example code. How do I format the code correctly/ what should I add to my code so that a different screen shows up when I flip the page?

Edit: my code

Home

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"/>

Upvotes: 0

Views: 493

Answers (1)

gunesevitan
gunesevitan

Reputation: 965

Try adding fragments to your adapter like this

    adapter.addFragment(new Fragment1(), "FRAG1");
    adapter.addFragment(new Fragment2(), "FRAG2");
    adapter.addFragment(new Fragment3(), "FRAG3");

then call it

    viewPager.setAdapter(adapter);

Can you also post activity home.xml?

Upvotes: 1

Related Questions