Convert Activity to View and image to View,
Package com. example. viewpager01;
Import java. util. ArrayList;
Import java. util. List;
Import android. app. Activity;
Import android. app. LocalActivityManager;
Import android. content. Context;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. support. v4.view. PagerAdapter;
Import android. support. v4.view. ViewPager;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. Window;
Import android. widget. ImageView;
Import android. widget. ImageView. ScaleType;
Public class MainActivity extends Activity {
Private ViewPager viewPager;
Private List <View> viewList;
Private View view1, view2, view3 ;//
// Context context = null;
LocalActivityManager manager = null; // here you need to convert Activity to View
Private int [] ids = {R. drawable. guide_page_1, R. drawable. guide_page_2, R. drawable. guide_page_3}; // stores the Image array. here you need to convert the image to View
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE );
SetContentView (R. layout. user_lader );
Manager = new LocalActivityManager (this, true );
Manager. dispatchCreate (savedInstanceState );
ViewList = new ArrayList <View> ();
For (int I = 0; I <ids. length; I ++ ){
ImageView iv = new ImageView (this );
Iv. setImageResource (ids [I]);
ViewGroup. LayoutParams params = new ViewGroup. LayoutParams (
ViewGroup. LayoutParams. FILL_PARENT,
ViewGroup. LayoutParams. FILL_PARENT );
Iv. setLayoutParams (params );
Iv. setScaleType (ScaleType. FIT_XY );
ViewList. add (iv );
}
Intent intent = new Intent (getApplicationContext (), A. class );
ViewList. add (getView ("A", intent ));
// 1,
ViewPager = (ViewPager) findViewById (R. id. view_pager );
// 2. Four functions that must be rewritten by PageAdapter:
/*
* Boolean isViewFromObject (View arg0, Object arg1) int getCount () void
* DestroyItem (ViewGroup container, int position, Object object) Object
* InstantiateItem (ViewGroup container, int position)
*/
ViewPager. setAdapter (new PagerAdapter (){
@ Override
Public int getCount (){
If (viewList = null)
{
Return 0;
}
Else
{
Return viewList. size (); // getCount (): returns the number of views to slide.
}
}
@ Override
Public boolean isViewFromObject (View arg0, Object arg1 ){
// Return arg0 = arg1; // isViewFromObject, we will rewrite it later.
// System. out. println ("position === isViewFromObject ====" + (int) Integer. parseInt (arg1.toString () + (arg0 = viewList. get (int) Integer. parseInt (arg1.toString ()))));
Return arg0 = viewList. get (int) Integer. parseInt (arg1.toString (); // locate the view based on the sent key and determine whether it is the same View as the passed parameter view arg0.
}
@ Override
Public Object instantiateItem (ViewGroup container, int position ){
Container. addView (viewList. get (position); // do two things: first: add the current View to the container, and second: return the current View
// Return viewList. get (position );
// System. out. println ("position = instantiateItem =" + position );
Return position; // you can specify the position of the newly added view as the Key.
}
@ Override
Public void destroyItem (ViewGroup container, int position,
Object object ){
If (position = 1)
{
ViewList = null;
}
Else
{
// System. out. println ("position = destroyItem =" + position );
Container. removeView (viewList. get (position); // Delete the View at the specified position from the current container;
}
}
});
}
Private View getView (String id, Intent intent ){
Return manager. startActivity (id, intent). getDecorView ();
}
}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.