Android uses ActionBar and ViewPager to switch pages

Source: Internet
Author: User

Android uses ActionBar and ViewPager to switch pages

The demo is as follows:

The project layout is as follows:

MainActivity. java code

Import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentManager; import android. support. v4.app. fragmentPagerAdapter; import android. support. v4.app. fragmentTransaction; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. support. v7.app. actionBar; import android. support. v7.app. actionBar. tab; import android. support. v7.app. actionBar. tabListener; import android. support. v7.app. actionBarActivity; import com. xuliugen. fragment. chatFragment; import com. xuliugen. fragment. friendFragment; import com. xuliugen. fragment. momentFragment; import com. xuliugen. fragment. moreFragment; public class MainActivity extends ActionBarActivity implements TabListener, OnPageChangeListener {private List
  
   
TabsList = new ArrayList
   
    
(4); private ViewPager viewPager; // ViewPager is used to store fragment private ActionBar actionBar; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // Add four fragment entries to the custom Tab to go to tabsList. add (new MyTab ("chat", ChatFragment. class); tabsList. add (new MyTab ("friend", FriendFragment. class); tabsList. add (new MyTab ("time", MomentFragment. class); tabsList. add (new MyTab ("more", MoreFragment. class); initActionBar ();} private void initActionBar () {// find viewpager viewPager = (ViewPager) this. findViewById (R. id. viewpager); actionBar = getSupportActionBar (); // obtain the ActionBar in v7. setNavigationMode (ActionBar. NAVIGATION_MODE_TABS); // set it to the navigation mode // Add the four tabs created to the actionBar through loop traversal for (MyTab Tab: tabsList) {tab t = actionBar. newTab (); // use the Tab t under v7. setText (tab. getText (); t. setTabListener (this); // sets the actionBar of the TabListener listener. addTab (t);} viewPager. setAdapter (new TabFragmentPagerAdapter (getSupportFragmentManager (); viewPager. setOnPageChangeListener (this);}/*** encapsulate the Class of a Tab ** the text of a tab ** the class contained in the tab */Class MyTab {private String text; // The title text private Class fragment; // The fragment public String getText () {return text;} public void setText (String text) {this. text = text;} public Class getFragment () {return fragment;} public void setFragment (Class fragment) {this. fragment = fragment;}/*** constructor for creating an object ** @ param string * @ param fragment */public MyTab (String string, Class fragment) {this. text = string; this. fragment = fragment; }}/ *** adapter set for viewpager */class TabFragmentPagerAdapter extends FragmentPagerAdapter {public TabFragmentPagerAdapter (FragmentManager fm) {super (fm );} @ Override public Fragment getItem (int I) {try {return (Fragment) tabsList. get (I ). getFragment (). newInstance ();} catch (InstantiationException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace () ;}return null ;}@ Override public int getCount () {return tabsList. size ();}} * is the method implemented by TabListener ******************* * ** // ** when the Tab is selected twice */@ Override public void onTabReselected (Tab tab, fragmentTransaction fragmentTransaction) {}/*** when the Tab is selected for a single time */@ Override public void onTabSelected (Tab tab, FragmentTransaction fragmentTransaction) {viewPager. setCurrentItem (tab. getPosition () ;}@ Override public void onTabUnselected (Tab tab, FragmentTransaction fragmentTransaction) {}/**************** below is the OnPageChangeListener implementation method *************** * // @ Override public void onPageScrollStateChanged (int I) {}@ Override public void onPageScrolled (int I, float arg1, int arg2) {}@ Override public void onPageSelected (int I) {actionBar. selectTab (actionBar. getTabAt (I ));}}
   
  

Below are four fragment

public class ChatFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_friends, container, false);    }}
public class FriendFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_chat, container, false);    }}
public class MomentFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_moment, container, false);    }}
public class MoreFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_more, container, false);    }}

Activity_main.xml


          
  

Below are four fragment layout files


  
      
       
   
  

The other three are the same, but the file name and content inside are changed.

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.