Im using this guide to implement a ViewPager:
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
This is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="@drawable/gradientbg" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"/>
</LinearLayout>
My main activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager)findViewById(R.id.viewPager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
//textviews and other controls that arent located in main.xml will be null for a couple of seconds here
The viewpager uses 2 child xml files as scrollable content. How do i know when the viewpager is complete so that i can make use of the child control, such as textviews etc?
Hope you get the idea, otherwise ill elaborate. Thanks in advance!
public class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 2;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.debug;
break;
case 1:
resId = R.layout.home;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
What I prefer to do in this kind of situation is write a custom
Viewimplementation that keeps references to its children which are initialized at creation. (I often just use inner static classes for that.) This helps to keep things modular.For example:
You get the drill. Now you could have a
setText(String)method like:In your case you would create two such view classes, one
HomeViewand oneDebugView(or whatever).You can either create the needed instances beforehand, which seems reasonable if both are being pre-cached by the ViewPager anyway, or you can create them on demand.
The first case (eager instantiation) could look like this:
You do all the needed initialization in the custom View class’ constructor, passing the required information (and/or maybe the
savedInstanceState) as required.Your PagerAdapter’s
instantiateItemcould then look like this:(you don’t want to use the
instantiateItem(View, int)method, it’s deprecated and unhandy.)If you want to instantiate your views on-demand, you you would move the constructor call into the adapter, then you will have to check for
nullvalues when accessing the views.If you can afford to target API 13 and above, you might want to use a FragmentPagerAdapter instead.
To make things look nicer, if you want a PagerAdapter that just servers a really limited number of views which have been pre-created (which you really only want if it’s a very small number like 2), you could write it like this:
which you could then instantiate using