Introduction to the android tab (3)

Source: Internet
Author: User

The previous section introduced the simple usage of tabactivity, but it is not recommended to use it in the current API. fragment is recommended to replace the above functions. The following describes how to use fragment and viewpager.

Http://blog.csdn.net/xia215266092/article/details/9613985

Tabactivity document.

This class was deprecated in API Level 13.
New applications shocould use fragments instead of this class; to continue to run on older devices, you can use the V4 support
Library which provides a version of the fragment API that is compatible downDONUT.

The following example is the use of android-support-v4.jar, write demo, this demo is also the API demo.

<android.support.v4.app.FragmentTabHost    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent">        <TabWidget            android:id="@android:id/tabs"            android:orientation="horizontal"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="0"/>        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0"/>        <FrameLayout            android:id="@+id/realtabcontent"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"/>    </LinearLayout></android.support.v4.app.FragmentTabHost>


import com.example.android.supportv4.R;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;/** * This demonstrates how you can implement switching between the tabs of a * TabHost through fragments, using FragmentTabHost. */public class FragmentTabs extends FragmentActivity {    private FragmentTabHost mTabHost;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.fragment_tabs);        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),                FragmentStackSupport.CountingFragment.class, null);        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),                LoaderCursorSupport.CursorLoaderListFragment.class, null);        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),                LoaderCustomSupport.AppListFragment.class, null);        mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),                LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);    }}

This is not the case today. fragment is the fragment after android4.0, Android. App. fragment, and V4 extension packages also contain fragment, but they are different.

The following is used for fragment after 4.0, the compiled version and mobile phone must be later than 4.0, Or I reference the android-support-v13.jar, this jar, not V4.

Package Org. liushui. tabdemo. frag; import Java. util. arraylist; import Java. util. list; import Org. liushui. tabdemo. r; import android. app. actionbar; import android. app. actionbar. tab; import android. app. activity; import android. app. fragment; import android. app. fragmenttransaction; import android. content. context; import android. OS. bundle; import android. support. v13.app. fragmentstatepageradapter; import android. suppo Rt. v4.view. viewpager; public class mainfragmentactivity extends activity {private viewpager mviewpager; private tabsadapter mtabsadapter; protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. frag_main); mviewpager = (viewpager) findviewbyid (R. id. PAGER); actionbar bar = getactionbar (); Bar. setnavigationmode (actionbar. navigation_mode_tabs); Bar. setd Isplayoptions (0, actionbar. display_show_title | actionbar. display_show_home); mtabsadapter = new tabsadapter (this, mviewpager); mtabsadapter. addtab (Bar. newtab (). settext ("phone"), afragment. class, null); mtabsadapter. addtab (Bar. newtab (). settext ("SMS"), bfragment. class, null); mtabsadapter. addtab (Bar. newtab (). settext ("dialing"), cfragment. class, null);} static class tabsadapter extends fragmentstatepageradapter I Mplements actionbar. tablistener, viewpager. onpagechangelistener {private context mcontext; private actionbar mactionbar; private viewpager mviewpager; private list <tabinfo> mtabs = new arraylist <tabinfo> (); static class tabinfo {private class <?> Clss; private bundle ARGs; private fragment; tabinfo (class <?> _ Class, bundle _ ARGs) {clss = _ class; ARGs = _ ARGs;} public tabsadapter (activity, viewpager pager) {super (activity. getfragmentmanager (); mcontext = activity; mactionbar = activity. getactionbar (); mviewpager = pager; mviewpager. setadapter (this); mviewpager. setonpagechangelistener (this); mviewpager. setoffscreenpagelimit (3);} public void addtab (actionbar. tab tab, class <?> Clss, bundle ARGs) {tabinfo info = new tabinfo (clss, argS); tab. settag (Info); tab. settablistener (this); mtabs. add (Info); mactionbar. addtab (Tab);} public int getcount () {return mtabs. size () ;}public int getitemposition (Object object) {return position_none;} public fragment getitem (INT position) {tabinfo info = mtabs. get (position); If (info. fragment = NULL) {info. fragment = fragment. instantiate (mcontext, info. clss. getname (), info. ARGs);} return info. fragment;} public void onpagescrolled (INT position, float positionoffset, int positionoffsetpixels) {} public void onpageselected (INT position) {mactionbar. setselectednavigationitem (position);} public void onpagescrollstatechanged (INT state) {} public void ontabselected (Tab tab, fragmenttransaction ft) {mviewpager. setcurrentitem (tab. getposition (), true);} public void ontabunselected (Tab tab, fragmenttransaction ft) {} public void ontabreselected (Tab tab, fragmenttransaction ft ){}}}

The layout file is very simple, that is, a viewpager. For viewpager, it is a bit similar to the launcher on the Android mobile phone and can slide between the left and right.

<?xml version="1.0" encoding="utf-8"?><android.support.v4.view.ViewPager    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/pager"    android:layout_width="match_parent"    android:layout_height="match_parent"></android.support.v4.view.ViewPager>

The sub-pages in the code above, afragment, bfragment, and cfragment all inherit fragment. Pay attention to the Android. App. fragment.

Package Org. liushui. tabdemo. frag; import Org. liushui. tabdemo. r; import android. app. fragment; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup;/*** title: afragment. java <br> * Author: xiaqiulei@gmail.com <br> * Date: July 29, 2013 <br> * version: V1.0 */public class afragment extends fragment {public view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {view = Inflater. inflate (R. layout. a, container, false); Return view ;}}

The oncreateview method returns an interface object, which is displayed in viewpager.


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.