Android Control Learning (4): combining ViewPager and TabHost with Java code, viewpagertabhost

Source: Internet
Author: User

Android Control Learning (4): combining ViewPager and TabHost with Java code, viewpagertabhost

Import java. util. ArrayList;

Import java. util. List;

Import android. app. LocalActivityManager;

Import android. app. TabActivity;

Import android. content. Context;

Import android. content. Intent;

Importandroid. graphics. BitmapFactory;

Import android. graphics. Matrix;

Import android. OS. Bundle;

Import android. OS. Parcelable;

Import android. support. v4.view. PagerAdapter;

Importandroid. support. v4.view. ViewPager;

Importandroid. support. v4.view. ViewPager. OnPageChangeListener;

Import android. util. DisplayMetrics;

Import android. util. Log;

Import android. view. View;

Import android. view. Window;

Importandroid. view. animation. Animation;

Importandroid. view. animation. TranslateAnimation;

Import android. widget. ImageView;

Import android. widget. TabHost;

Import android. widget. TextView;

Public class AniTestActivity extends TabActivity {

// Page card content

Private ViewPager mPager;

// Tab page list

Private List listViews;

// Animated Image

Private ImageView cursor;

// Tab

Private TextView t1, t2, t3;

// Specifies the animation offset.

Private int offset = 0;

// Current page card number

Private int currIndex = 0;

// Animation Image Width

Private int bmp w;

Private LocalActivityManager manager = null;

Private final static String TAG = "ConfigTabActivity ";

Private final Context context = AniTestActivity. this;

Private TabHost mTabHost;

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Log. d (TAG, "--- onCreate ---");

RequestWindowFeature (Window. FEATURE_NO_TITLE );

SetContentView (R. layout. main );

MTabHost = getTabHost ();

MTabHost. addTab (mTabHost. newTabSpec ("A"). setIndicator (

""). SetContent (

New Intent (this, A. class )));

MTabHost. addTab (mTabHost. newTabSpec ("B"). setIndicator (

""). SetContent (

New Intent (this, B. class )));

MTabHost. addTab (mTabHost. newTabSpec ("C"). setIndicator (

""). SetContent (

New Intent (this, C. class )));

MTabHost. setCurrentTab (0 );

Manager = new LocalActivityManager (this, true );

Manager. dispatchCreate (savedInstanceState );

InitImageView ();

InitTextView ();

InitViewPager ();

}

Private void InitTextView (){

T1 = (TextView) findViewById (R. id. text1 );

T2 = (TextView) findViewById (R. id. text2 );

T3 = (TextView) findViewById (R. id. text3 );

T1.setOnClickListener (newMyOnClickListener (0 ));

T2.setOnClickListener (newMyOnClickListener (1 ));

T3.setOnClickListener (newMyOnClickListener (2 ));

}

Private void InitViewPager (){

MPager = (ViewPager) findViewById (R. id. vPager );

ListViews = new ArrayList ();

MyPagerAdapter mpAdapter = new MyPagerAdapter (listViews );

Intent intent = new Intent (context, A. class );

ListViews. add (getView ("Black", intent ));

Intent intent2 = new Intent (context, B. class );

ListViews. add (getView ("Gray", intent2 ));

Intent intent3 = new Intent (context, C. class );

ListViews. add (getView ("White", intent3 ));

MPager. setAdapter (mpAdapter );

MPager. setCurrentItem (0 );

MPager. setOnPageChangeListener (newMyOnPageChangeListener ());

}

Private void InitImageView (){

Cursor = (ImageView) findViewById (R. id. cursor );

Bmp w = BitmapFactory. decodeResource (getResources (), R. drawable. a4)

. GetWidth (); // obtain the image width.

DisplayMetrics dm = new DisplayMetrics ();

GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

Int screenW = dm. widthPixels; // gets the resolution width.

Offset = (screenW/3-bmp w)/2; // calculates the offset.

Matrix matrix = new Matrix ();

Matrix. postTranslate (offset, 0 );

Cursor. setImageMatrix (matrix); // you can specify the initial animation position.

}

Public class MyPagerAdapter extends PagerAdapter {

Public List mListViews;

PublicMyPagerAdapter (List mListViews ){

This. mListViews = mListViews;

}

@ Override

Public void destroyItem (View arg0, int arg1, Object arg2 ){

(ViewPager) arg0). removeView (mListViews. get (arg1 ));

}

@ Override

Public void finishUpdate (View arg0 ){

}

@ Override

Public int getCount (){

Return mListViews. size ();

}

@ Override

Public Object instantiateItem (View arg0, int arg1 ){

(ViewPager) arg0). addView (mListViews. get (arg1), 0 );

Return mListViews. get (arg1 );

}

@ Override

Public boolean isViewFromObject (View arg0, Object arg1 ){

Return arg0 = (arg1 );

}

@ Override

Public void restoreState (Parcelable arg0, ClassLoader arg1 ){

}

@ Override

Public Parcelable saveState (){

Return null;

}

@ Override

Public void startUpdate (View arg0 ){

}

}

Public class MyOnClickListener implementsView. OnClickListener {

Private int index = 0;

Public MyOnClickListener (int I ){

Index = I;

}

@ Override

Public void onClick (View v ){

MPager. setCurrentItem (index );

}

};

Public class MyOnPageChangeListener implementsOnPageChangeListener {

Int one = offset * 2 + bmp w; // page Card 1-> page Card 2 offset

Int two = one * 2; // page Card 1-> page card 3 offset

@ Override

Public void onPageSelected (int arg0 ){

Animation animation = null;

Intent intent = new Intent ();

Switch (arg0 ){

Case 0:

Log. d (TAG, "--- 0 ---");

MTabHost. setCurrentTab (0 );

If (currIndex = 1 ){

Animation = newTranslateAnimation (one, 0, 0, 0 );

} Else if (currIndex = 2 ){

Animation = newTranslateAnimation (two, 0, 0 );

}

Break;

Case 1:

Log. d (TAG, "--- 1 ---");

MTabHost. setCurrentTab (1 );

If (currIndex = 0 ){

Animation = newTranslateAnimation (offset, one, 0, 0 );

} Else if (currIndex = 2 ){

Animation = new TranslateAnimation (two, one, 0, 0 );

}

Break;

Case 2:

Log. d (TAG, "--- 2 ---");

MTabHost. setCurrentTab (2 );

If (currIndex = 0 ){

Animation = newTranslateAnimation (offset, two, 0, 0 );

} Else if (currIndex = 1 ){

Animation = newTranslateAnimation (one, two, 0, 0 );

}

Break;

}

CurrIndex = arg0;

Animation. setFillAfter (true); // True: the image stops at the animation end position.

Animation. setDuration (300 );

Cursor. startAnimation (animation );

}

@ Override

Public void onPageScrolled (int arg0, float arg1, int arg2 ){

}

@ Override

Public void onPageScrollStateChanged (int arg0 ){

}

}

Private View getView (String id, Intent intent)

{

Return manager. startActivity (id, intent). getDecorView ();

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.