Android Rotation page +tabhost instance

Source: Internet
Author: User

The final effect shows:

First we need a Viewpager control, but we can see that there is no control in the list of controls on the left

Then we're going to go to the upgrade package to see

And then we find viewpager.class in centimeters, and then we double-click the discovery. Cannot view source code

We can copy it to the Libs directory by using a file of Android-support-v4.jar.properties and double-click to open

The following code appears:

src = e:\\adt-bundle-windows-x86_64-20140702\\sdk\\sources

Change the path to the corresponding sources directory under your SDK to note that the \ symbol is denoted by the escape character \ \!

After completing the above work, close the project and reload it to open the source code of the upgrade package!

Android-support-v4.jar.properties:

Http://files.cnblogs.com/files/Laopengblog/%E6%9F%A5%E7%9C%8Bsupport.v4%E6%BA%90%E4%BB%A3%E7%A0%81.rar

Let's open Viewpager.class and copy the package name Android.support.v4.view use it as the name of the control

Attach the complete main layout code

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.huarui.my12android2502.MainActivity" > <tabhost Android:id= "@android: Id/tabhost"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <Tabwidget Android:id= "@android: Id/tabs"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > </TabWidget> <framelayout Android:id= "@android: Id/tabcontent"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <!--it doesn't really work to make up a space--<LinearLayout Android:id= "@+id/tab1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "vertical" > </LinearLayout> </FrameLayout> </LinearLayout> </ Tabhost> <Android.support.v4.view.ViewPager Android:id= "@+id/vp"Android:layout_width= "Fill_parent"Android:layout_height= "Match_parent" > </android.support.v4.view.ViewPager></LinearLayout>
Activity_main.xml

We're creating 3 layouts for switching between them (feel free to create, I just put a butten a little bit apart, the code is omitted)

Activity_view1.xml

Activity_view2.xml

Activity_view3.xml

and start writing the main code. Important comments are in the code.

 Packagecom.huarui.my12android2502;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;Importandroid.app.TabActivity;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.support.v4.view.ViewPager.OnPageChangeListener;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TabHost;ImportAndroid.widget.TabHost.OnTabChangeListener;ImportAndroid.widget.TabHost.TabSpec; Public classMainactivityextendstabactivity {//v//Page Rotation Controls    PrivateViewpager VP; PrivateTabhost Tabhost; //m Save the ID of the page    Private int[] Pageids =New int[]{r.layout.view1, R.layout.view2, r.layout.view3}; //C Adapter    PrivatePageradapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Initview (); }    Private voidInitview () {Setcontentview (r.layout.activity_main);                Initviewpager (); Tabhost= This. Gettabhost (); AddTab ("0", "first page"); AddTab ("1", "second page"); AddTab ("2", "third page"); //I'm going to trigger an event after the tab check is changed.Tabhost.setontabchangedlistener (NewOntabchangelistener () {@Override//the tab change will trigger this method//tabId             Public voidontabchanged (String tabId) {//TODO auto-generated Method Stub//I got the tab of the check mark .                intTabIndex =Integer.parseint (tabId); //let the view rotation control price show the figure of TabIndexVp.setcurrentitem (TabIndex);    }        }); }    Private voidAddTab (String tag,string title) {Tabspec Spec=Tabhost.newtabspec (tag);        Spec.setindicator (title);        Spec.setcontent (R.ID.TAB1);    Tabhost.addtab (spec); }    Private voidInitviewpager () {//vVP =(Viewpager) Findviewbyid (R.ID.VP); //m//Cadapter =NewMyPageAdapter (); //VCVp.setadapter (adapter); //This event is triggered when the sliding rotation control changes the pageVp.setonpagechangelistener (NewOnpagechangelistener () {@Override//position the subscript of the selected page             Public voidOnpageselected (intposition) {                //TODO auto-generated Method StubTabhost.setcurrenttab (position); } @Override Public voidOnpagescrolled (intPositionfloatPositionoffset,intpositionoffsetpixels) {                //TODO auto-generated Method Stub} @Override Public voidOnpagescrollstatechanged (intState ) {                //TODO auto-generated Method Stub                            }        }); }        Private classMyPageAdapterextendspageradapter{PrivateList<view>vs;  PublicMyPageAdapter () {vs=NewArraylist<view>();  for(inti=0;i<pageids.length;i++) {View View= View.inflate (mainactivity. This, Pageids[i],NULL);            Vs.add (view); }} @Override//tell the adapter how many pages to rotate         Public intGetCount () {//TODO auto-generated Method Stub            returnpageids.length; } @Override Public Booleanisviewfromobject (View view, Object object) {//TODO auto-generated Method Stub//Google asked to write this            returnview==object; } @Override//Initialize an entry (this method needs to be added by itself: Right-click-source-override/...-to find this method tick OK)//container is Viewpager itself .//position's coming out. The following table of views         PublicObject Instantiateitem (ViewGroup container,intposition) {            //TODO auto-generated Method Stub//return Super.instantiateitem (container, position); //View view = View.inflate (Mainactivity.this, pageids[position], NULL); //Add the view to the rotation containerContainer.addview (vs.get (position)); //returns the added view as the return value            returnVs.get (position); } @Override//Destroy the entry (this method needs to add the method yourself: Right-click-source-override/...-Find this method tick OK)//container container itself//Position the subscript of the view to be destroyed//object is about to be destroyed page         Public voidDestroyitem (ViewGroup container,intposition, Object object) {            //TODO auto-generated Method Stub//Super.destroyitem (container, Position, object);Container.removeview ((View) object); }                            }    }

Android Rotation page +tabhost instance

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.