Android -------------------- source code implementation for switching between ActionBar and ViewPager and ActionTab

Source: Internet
Author: User
Tags transparent color

Android -------------------- source code implementation for switching between ActionBar and ViewPager and ActionTab



Reference URL:

Click Open Link


Source code implementation:

Package com. example. actionbardemo2; import android. app. actionBar; import android. app. actionBar. tab; import android. graphics. color; import android. graphics. drawable. colorDrawable; import android. graphics. drawable. drawable; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentManager; import android. support. v4.ap P. fragmentPagerAdapter; import android. support. v4.view. viewPager; import android. view. menu; import android. view. menuItem; public class MainActivity extends FragmentActivity implementsActionBar. tabListener {private partition fragment1 = new partition (); private dFragment2 fragment2 = new dFragment2 (); private partition fragment3 = new dFragment3 (); private static final int TAB_INDEX_COUNT = 2; private s Tatic final int TAB_INDEX_ONE = 0; private static final int TAB_INDEX_TWO = 1; private ViewPager viewPager; private myFrgamentPagerAdapter viewPagerAdapter; @ Overrideprotected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // 1 sets ViewPagersetViewPager (); // 2 gets ActionBargetActionBarMethod ();} // 1 sets ViewPagerprivate void setViewPager () {ViewPagerAdapter = new myFrgamentPagerAdapter (getSupportFragmentManager (); viewPager = (ViewPager) this. findViewById (R. id. viewpager); viewPager. setAdapter (viewPagerAdapter); viewPager. setOnPageChangeListener (new ViewPager. simpleOnPageChangeListener () {@ Overridepublic void onPageSelected (int position) {// TODO Auto-generated method stubsuper. onPageSelected (position); final ActionBar actionBar = getA CtionBar (); actionBar. setSelectedNavigationItem (position) ;}@ Overridepublic void onPageScrollStateChanged (int state) {// TODO Auto-generated method stubsuper. onPageScrollStateChanged (state) ;}}) ;}// 2 get ActionBarprivate void getActionBarMethod () {// get ActionBarActionBar actionBar = getActionBar (); actionBar. setNavigationMode (ActionBar. NAVIGATION_MODE_TABS); // sets the actionBar color Drawable draw = new ColorDraw. Able (Color. GREEN); actionBar. setBackgroundDrawable (draw); // sets the ActionBar tabActionBar. tab tab = actionBar. newTab (); tab. setText ("core products"); tab. setIcon (draw); tab. setTabListener (this); actionBar. addTab; tab = actionBar. newTab (); tab. setText ("platform product"); tab. setIcon (draw); tab. setTabListener (this); actionBar. addTab; // remove the icon on the right of the Small arrow on the left: // an actionBar on the left. setDisplayHomeAsUpEnabled (true); // indicates that LogoactionBa is disabled. R. setDisplayUseLogoEnabled (false); // use a transparent color to replace the original icon, which is equivalent to not displaying the actionBar. setIcon (new ColorDrawable (Color. TRANSPARENT);}/*** ActionBar action Click Event Processing: **/@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// TODO Auto-generated method stubswitch (item. getItemId () {case android. r. id. home: finish (); return true;} return super. onOptionsItemSelected (item);}/*** ActionTab Click Event: ***/@ Overrid Epublic void onTabReselected (Tab arg0, android. app. fragmentTransaction arg1) {// TODO Auto-generated method stub} @ Overridepublic void onTabSelected (Tab tab, android. app. fragmentTransaction arg1) {// TODO Auto-generated method stub // set the current display page of ViewPager Based on the index of the ActionBar Tab. ViewPager. setCurrentItem (tab. getPosition () ;}@ Overridepublic void onTabUnselected (Tab arg0, android. app. fragmentTransaction arg1) {// TODO Auto-generated method stub}/*** ActionBar style, set. * **/@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} // TODO 2 custom Viewpager class: class myFrgamentPagerAdapter extends FragmentPagerAdapter {public myFrgamentPagerAdapter (FragmentManager fm) {super (fm ); // TODO Auto-generated constructor stub} @ Overridepublic Fragment getItem (int position) {// switch (position) {case TAB_INDEX_ONE: return fragment1; case TAB_INDEX_TWO: return fragment2;} throw new IllegalStateException ("No fragment at position" + position);} @ Overridepublic int getCount () {// TODO Auto-generated method stubreturn TAB_INDEX_COUNT ;}}}


Related Article

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.