2016-02-29 3 views
1

J'essaie de faire un pager image et il semble que je l'ai fait correct selon tous les tutoriels que j'ai recherchés, mais tout ce que je reçois est un écran vide. Un point d'arrêt sur la méthode instantiateItem de l'adaptateur m'indique qu'il est appelé et que toutes les bonnes informations sont définies dans les vues, et que le balayage fonctionne même, mais je ne vois toujours rien. Voici mon codePagerAdapter retourner vide toujours

activity_photos.xml

<RelativeLayout> // I'm not including that code, irrelevant. 
<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/imagePager" 
    android:background="@android:color/black"/> 
</RelativeLayout> 

viewpager_itemx.xml

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

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:id="@+id/imageView"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageLabel" 
     android:textColor="@android:color/white"/> 
</LinearLayout> 

PhotosActivity.java

final String[] images = getResources().getStringArray(R.array.tips_images); 
    final String[] labels = getResources().getStringArray(R.array.tips_text); 

    final ViewPager viewPager = (ViewPager) findViewById(R.id.imagePager); 
    final ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(PhotoTipsActivity.this, images, labels); 

    viewPager.setAdapter(viewPagerAdapter); 

et enfin, ViewPagerAdapter.java

ViewPagerAdapter public class étend PagerAdapter {

private Context context; 
private String[] images; 
private String[] labels; 

public ViewPagerAdapter(Context context, String[] images, String[] labels) { 
    this.context = context; 
    this.images = images; 
    this.labels = labels; 
} 

@Override 
public int getCount() { 
    return images.length; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == object; 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    final View itemView = LayoutInflater.from(context).inflate(R.layout.viewpager_item, container, false); 
    final ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView); 
    final TextView imageLabel = (TextView) itemView.findViewById(R.id.imageLabel); 

    // Get drawable image. 
    final int imageId = context.getResources().getIdentifier(images[position], "drawable", context.getPackageName()); 

    imageView.setImageResource(imageId); 
    imageLabel.setText(labels[position]); 

    return itemView; 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    container.removeView(((LinearLayout) object)); 
} 

}

Quel est le motif tout à fait évident pourquoi je ne vois pas mes images?

Répondre

3

De la même manière destroyItem() doit supprimer la vue du conteneur, instantiateItem() doit ajouter la vue au conteneur.

Ajoutez juste

container.addView(itemView); 

avant le retour de instantiateItem() et vous serez dans les affaires.

+1

Je ne peux pas croire que j'ai raté ça! –