Android rotation page + TabHost instance, androidtabhost
Final effect display:
First, we need a ViewPager control, but we can find that this control is not in the Control List on the left.
Now we need to go to the upgrade package to view
Then find ViewPager. class in cm. Then we double-click this to find that the source code cannot be viewed.
We can copy it to the libs directory with a android-support-v4.jar.properties file and double-click it to open
The following code is displayed:
Src = E: \ adt-bundle-windows-x86_64-20140702 \ sdk \ sources
Change the path to the corresponding sources directory under your SDK. Note that the \ symbol must be expressed as an escape character!
After completing the above work, close the project and re-Load 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
Next we open ViewPager. class and copy the package name android. support. v4.view to use it as the control name.
Complete Master Layout code is attached.
<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_widt H = "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 is useless to occupy 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 are creating three la s for switching between them (you can just create them at will. I just put a Butten to differentiate them, and the code will be omitted)
Activity_view1.xml
Activity_view2.xml
Activity_view3.xml
Then we started to write important comments for the main code in the code.
Package com. huarui. my12android2502; import java. util. arrayList; import java. util. list; import android. app. activity; import android. app. tabActivity; import android. OS. bundle; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. viewGroup; import android. widget. tabHost; import android. widget. tabHost. onTabChangeListener; import android. widget. tabHost. tabSpec; public class MainActivity extends TabActivity {// v // page rotation control private ViewPager vp; private TabHost tabHost; // m Save the page ID private int [] pageids = new int [] {R. layout. view1, R. layout. view2, R. layout. view3}; // c adapter private PagerAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); initview ();} private void initview () {setContentView (R. layout. activity_main); initViewpager (); tabHost = this. getTabHost (); addtab ("0", "first page"); addtab ("1", "second page"); addtab ("2", "Third page "); // when the Selected tab changes, I will trigger an event tabHost. setOnTabChangedListener (new OnTabChangeListener () {@ Override // This method is triggered when the Selected tab changes // tabId public void onTabChanged (String tabId) {// TODO Auto-generated method stub // The Selected tab's small int tabindex = Integer. parseInt (tabId); // sets the view rotation to show the graph vp of the tabindex. setCurrentItem (tabindex) ;}}) ;}private void addtab (String tag, String title) {TabSpec spec = tabHost. newTabSpec (tag); spec. setIndicator (title); spec. setContent (R. id. tab1); tabHost. addTab (spec);} private void initViewpager () {// v vp = (ViewPager) findViewById (R. id. vp); // m // c adapter = new MyPageAdapter (); // vc vp. setAdapter (adapter); // This event vp is triggered when the sliding rotation control changes the page. setOnPageChangeListener (new OnPageChangeListener () {@ Override // The subscript of the position selected page public void onPageSelected (int position) {// TODO Auto-generated method stub tabHost. setCurrentTab (position) ;}@ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {// TODO Auto-generated method stub} @ Override public void merge (int state) {// TODO Auto-generated method stub});} private class MyPageAdapter extends PagerAdapter {private List <View> vs; public MyPageAdapter () {vs = new ArrayList <View> (); for (int I = 0; I <pageids. length; I ++) {View view = View. inflate (MainActivity. this, pageids [I], null);. add (view) ;}@ Override // tell the adapter the total number of public int getCount () {// TODO Auto-generated method stub return pageids. length ;}@ Override public boolean isViewFromObject (View view, Object object) {// TODO Auto-generated method stub // Google requires that return view = object ;} @ Override // Initialize an entry (you need to add this method by yourself: Right-click-Source-Override /... -Locate this method and check) // container is the public Object instantiateItem (ViewGroup container, int position) in the following table of views to be displayed by viewpager itself // position) {// 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 container to remove container. addView (. get (position); // return the return value of the added view as the return value. get (position) ;}@ Override // destroy the entry (this method needs to be added by yourself: Right-click-Source-Override /... -Locate this method and check) // iner container itself // position subscript of the view to be destroyed // page public void destroyItem (ViewGroup container, int position, object object) {// TODO Auto-generated method stub // super. destroyItem (container, position, object); container. removeView (View) object );}}}