This is tested and working.
I have implemented the 3 views as TextViews but could be any layout, then you just have to make changes accordingly.
public class MainActivity extends Activity {
ViewPager myPager;
MyPagerAdapter adapter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MyPagerAdapter(getLayoutInflater());
myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override protected void onResume() {
super.onResume();
TextView hotelAddressView = adapter
.getViewAtPosition(MyPagerAdapter.POSITION_ADDRESS);
hotelAddressView.setText("modified");
}
private class MyPagerAdapter extends PagerAdapter {
public static final int POSITION_MAP = 0;
public static final int POSITION_ADDRESS = 1;
public static final int POSITION_CONTACT = 2;
private TextView hotelMapView = null;
private TextView hotelAddressView = null;
private TextView hotelContactView = null;
public MyPagerAdapter(LayoutInflater inflater) {
hotelMapView = (TextView) inflater.inflate(R.layout.hotelmap, null);
hotelAddressView = (TextView) inflater.inflate(
R.layout.hoteladdress, null);
hotelContactView = (TextView) inflater.inflate(
R.layout.hotelcontact, null);
}
public int getCount() {
return 3;
}
public TextView getViewAtPosition(int position) {
Log.d("Main", "getViewAtPosition " + position);
TextView view = null;
switch (position) {
case POSITION_MAP:
view = hotelMapView;
break;
case POSITION_ADDRESS:
view = hotelAddressView;
break;
case POSITION_CONTACT:
view = hotelContactView;
break;
}
return view;
}
public Object instantiateItem(ViewGroup collection, int position) {
View view = getViewAtPosition(position);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override public void destroyItem(ViewGroup collection, int position,
Object view) {
((ViewPager) collection).removeView((TextView) view);
}
@Override public boolean isViewFromObject(View view, Object object) {
return view == ((TextView) object);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…