I've come up with a solution that solves the problem as easily as I wanted. 
Everything is working as expected... I just want to understand if this is a 
good solution or there's something wrong doing it this way...

Every ViewPager tutorial/document (for instance, an official one: 
http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html)
 
demonstrates the instantiateItem() method as the one responsible for 
initializing View components, either by code or by inflating layouts.

My solution inflates the needed layouts in the PagerAdapter constructor 
instead of the instantiateItem() method. This way, I can keep a reference 
to those layouts (I'm using an ArrayList) and access them whenever I want. 
Now, on the instantiateItem() method, instead of inflating any layout I 
just pick the one I need, like this:

@Override
public Object instantiateItem(View container, int position) {
    View view = mPageViews.get(position);

    ((ViewPager)container).addView(view, 0);

    return view;
}

I've also added a public method to the PagerAdapter to find the Views I 
want, like this:

public View findViewById(int position, int id) {
    return mPageViews.get(position).findViewById(id);
}

This solves the problem. As an example of the small code changes I have to 
do on the Activity's onCreate() method, I just replace this:

mTextTitle = (EditText)findViewById(R.id.edittext_title);

With this:

mTextTitle = 
(EditText)editorAdapter.findViewById(NoteEditorAdapter.PAGE_NAME_1, 
R.id.edittext_title);

What do you think? Is there anything wrong in inflating the layouts in the 
PagerAdapter's constructor instead of the instantiateItem() method or it's 
an acceptable solution?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to