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;
}
I've recently implemented this and this is my
instantiateItem
method (made it a bit minimal for readability.So for the page that is displayed, I get the data from my
evaluations
list using the position as the index. Then inflate theLayout
which has the views I will add my data too. Then I get theTextView
to set the evaluation summary text on. Then the wholeLayout
is added to theViewPager
. And finally theLayout
is also returned.If you still can't get it post your code.
I have the same misunderstanding how this adapter works and I have made some investigation.
The key particularity is that your views are stored in two places:
1 in ViewPager you add them by calling
((ViewPager) container).addView(view);
(here you need to store only three views, what you see and left and right neighborhoods)2 in
private final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
this is member of ViewPager there are stored views with information about they possition (they are added here by calling adapter methodmAdapter.instantiateItem(this, position);
)When you slides
ViewPager
gets view position frommItems
array or instantiates it and compare this view with children ofViewPager
with adapters methodpublic boolean isViewFromObject(View view, Object object)
. View which is equals toobject
is displayed to user onViewPager
. If there is no view then the blank screen is displayed.Here is
ViewPager
method where view is compared to object:If views position from mItems is not in range {currentposition -1,currentposition +1} then it will be destroyed:
the view of ViewPagers memory 1
It is one trap with
destroyItem
when you slides forward firs is calleddestroyItem
and then new item is added, but when you slides backward first is added new item and then old is destroyed. If you try to use only three cached views you can getIllegalStateException: The specified child already has a parent.
while sliding backwards.