So far I've been hacking together examples to get a feel for the APIs the Android SDK as a whole. However, I've hit an impasse.
I'm trying to Inflate a LinearLayout with 2 TextViews from an XML file. These will be then Views that are paged. I simply can't get this to work and realize that I totally don't understand what's going on with the code.
Looking at the source I can see that the method ViewPager.addNewItem calls the InstantiateItem from the supplied adapter. And addNewItem is called in populate()
. populate() is called in a number of other places.
Anyway, in the example, I've experienced that when the method is overridden for PagerAdapter one must include a call to addView()
on the ViewPager collection that is passed from the ViewPager
as an argument.
If I wish to add multiple views I thought this would be a case of calling addView() more than once but as instantiateItem() returns an Object to ViewPager (in the example I had it returns the view that it added) I don't know what to return. I've tried returning the inflated view and a number of other things.
Please, can someone explain what is happening here?
Much appreciated.
@Override
public Object instantiateItem(View collection, int position) {
layout = inflater.inflate(R.layout.animallayout, null);
TextView tv = (TextView) layout.findViewById(R.id.textView1);
tv.setText("1________________>" + position);
TextView tv2 = (TextView) layout.findViewById(R.id.textView2);
tv.setText("2________________>" + position);
((ViewPager) collection).addView(layout);
return layout;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…