0

J'ai une bizarrerie intéressante que je n'arrive pas à comprendre. J'ai donc enveloppé ma mise en page autour d'un widget afin que je puisse ajouter quelques événements et d'autres méthodes pratiques. Cependant, en utilisant le constructeur du widget, je suis incapable de l'instancier en tant qu'enfant dans un ViewPager. Cependant, si je gonfle la mise en page sans l'enveloppe du widget, cela fonctionne.Les enfants Android ViewPager ne sont pas montrés lorsqu'ils sont instanciés en utilisant un widget composite plutôt qu'une mise en page gonflée

Heres le code qui fonctionne: (Celui qui ne fonctionne pas est commenté)

private View getNewTabView() { 
    View view = getLayoutInflater().inflate(
      R.layout.checkpoint_new_tab_view, null); 

    // XmlPullParser parser = getResources().getXml(
    // R.layout.checkpoint_new_tab_view); 
    // AttributeSet as = Xml.asAttributeSet(parser); 
    // NewCheckpointsTabVIew view = new NewCheckpointsTabVIew(this, as); 
    return view; 
} 

Voici mon code adaptateur pour mon ViewPager

private class CheckpointPagerAdapter extends PagerAdapter { 

    @Override 
    public void destroyItem(View collection, int position, Object view) { 
     ((ViewPager) collection).removeView((ScrollView) view); 
    } 

    @Override 
    public void finishUpdate(View arg0) { 
    } 

    @Override 
    public int getCount() { 
     return 4; 
    } 

    @Override 
    public Object instantiateItem(View arg0, int arg1) { 
     View child = getNewTabView(); 
     ((ViewPager) arg0).addView(child, 0); 
     return child; 
    } 

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

    @Override 
    public void restoreState(Parcelable arg0, ClassLoader arg1) { 
    } 

    @Override 
    public Parcelable saveState() { 
     return null; 
    } 

    @Override 
    public void startUpdate(View arg0) { 
    } 

} 

Et Heres l'enveloppe pour le widget qui entoure la disposition gonflée:

public class NewCheckpointsTabVIew extends ScrollView { 

    Context context; 

    public NewCheckpointsTabVIew(Context context) { 
     super(context); 
     this.context = context; 
    } 

    public NewCheckpointsTabVIew(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context = context; 
    } 

    private void init() { 
     // height + width 
     android.view.ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = android.view.ViewGroup.LayoutParams.FILL_PARENT; 
     params.width = android.view.ViewGroup.LayoutParams.FILL_PARENT; 
     setLayoutParams(params); 
    } 

    @Override 
    public void onFinishInflate() { 
     super.onFinishInflate(); 
     LayoutInflater layoutInflater = (LayoutInflater) getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     layoutInflater.inflate(R.layout.checkpoint_new_tab_view, this); 
    } 

    @Override 
    public void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     init(); 
    } 

} 

Est-ce que quelqu'un sait pourquoi? Merci!

Répondre

Questions connexes