Je suivais exemple de l'exemple doc Android ici: https://developer.android.com/training/animation/screen-slide.htmlRemplacer Fragment dans ViewPager
Je me demandais si je veux plusieurs fragments différents sur chaque page, alors ce serait la meilleure approche.
Ceci est mon MainActivity, comme vous pouvez le voir, j'ouvre une nouvelle classe de fragments pour chaque position de la page:
public class MainActivity extends AppCompatActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 4;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new MainActivity.ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents 4 HeartsPageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new HeartsPageFragment();
case 1:
return new DiamondsPageFragment();
case 2:
return new ClubsPageFragment();
case 3:
return new SpadesPageFragment();
}
return null;
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
Ceci est l'une des classes de fragments, les quatre sont presque identiques:
public class ClubsPageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.view_clubs, container, false);
return rootView;
}
}
Je me demandais s'il était préférable d'avoir quatre classes de fragments différentes pour chaque page? J'ai l'impression que c'est répétitif et mauvais, mais je ne suis pas sûr de la façon dont je pourrais régler ça. Il est en retard sur mon téléphone lorsque je passe d'un téléphone à l'autre. Tous les conseils seraient les bienvenus.
Edit:
Voici le code pour la mise en page.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content_suit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/first_row_hearts">
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView1"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_2"/>
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView2"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_3"/>
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView3"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_4"/>
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView4"
android:src="@drawable/spades_5"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/second_row_hearts"
android:layout_below="@id/first_row_hearts"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView5"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_6" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView6"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_7" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView7"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_8" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView8"
android:src="@drawable/spades_9" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/third_row_hearts"
android:layout_below="@id/second_row_hearts"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView9"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_10" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView10"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_jack" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView11"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_queen" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView12"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_king"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fourth_row_hearts"
android:layout_below="@+id/third_row_hearts"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="@+id/imageView13"
android:layout_marginEnd="10dp"
android:src="@drawable/spades_ace"/>
</LinearLayout>
</RelativeLayout>
Ce qui est en retard exactement? J'ai eu un ViewPager de 13 pages avant et aucun décalage. –
J'ai 13 images sur chaque page, il y a 4 pages en tout. Et la mise en page a RelativeLayout qui a 4 LinearLayouts à l'intérieur. Chaque image a une taille de 8kB. – neX
Utilisez-vous une bibliothèque de chargement d'images pour afficher ces images? Sinon, les bitmaps consomment de la mémoire rapidement. À tout moment dans le ViewPager, vous avez 39 images en mémoire. –