Android 導航類型

來源:互聯網
上載者:User

下拉式導航:

    final ActionBar actionBar = getSupportActionBar();        //設定ActionBar是否顯示標題        actionBar.setDisplayShowTitleEnabled(false);        //設定導航模式,使用List導航        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);        // 為導航設定清單項目資料來源和監聽器        actionBar.setListNavigationCallbacks(                // Specify a SpinnerAdapter to populate the dropdown list.                new ArrayAdapter(//為導航設定清單項目                        actionBar.getThemedContext(),                        android.R.layout.simple_list_item_1,                        android.R.id.text1,                        new String[] {                                getString(R.string.title_section1),                                getString(R.string.title_section2),                                getString(R.string.title_section3),                        }),                this);//這個this為導航設定監聽器ActionBar.OnNavigationListener,如下所示                          //當導航被選中時激發該方法@Overridepublic boolean onNavigationItemSelected(int position, long id) {    // When the given dropdown item is selected, show its contents in the    // container view.    getSupportFragmentManager().beginTransaction()            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))            .commit();    return true;}//內部類,根據所選列表id動態建立並返回的Fragment類public static class PlaceholderFragment extends Fragment {}

ActionBar實現Tab導航:

//設定ActionBar 的Tabs導航        final ActionBar actionBar = getSupportActionBar();        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        //FragmentPaperAdapter對象(下面附上),這個適配器根據選擇返回對應的Fragment        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());//ViewPaper是Fragment的容器,可以同時管理多個Fragment,並允許多個Fragment切換時提供動畫效果,需要為它設定適配器FragmentPagerAdapter        // Set up the ViewPager with the sections adapter.        mViewPager = (ViewPager) findViewById(R.id.pager);        mViewPager.setAdapter(mSectionsPagerAdapter);        // 為ViewPaper設定監聽器,當ViewPaper顯示的Fragment發生改變時激發該方法        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {            @Override            public void onPageSelected(int position) {                actionBar.setSelectedNavigationItem(position);            }        });        // 遍曆paperAdapter對象所包含的全部Fragment,每個Fragment對應建立一個Tab標籤,並設定ActionBar的事件監聽介面對象TabListener        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {            // Create a tab with text corresponding to the page title defined by            // the adapter. Also specify this Activity object, which implements            // the TabListener interface, as the callback (listener) for when            // this tab is selected.            actionBar.addTab(                    actionBar.newTab()                            .setText(mSectionsPagerAdapter.getPageTitle(i))                            .setTabListener(this)            );        }        public class SectionsPagerAdapter extends FragmentPagerAdapter {    public SectionsPagerAdapter(FragmentManager fm) {        super(fm);    }擷取第position位置的Fragment    @Override    public Fragment getItem(int position) {        // getItem is called to instantiate the fragment for the given page.        // Return a PlaceholderFragment (defined as a static inner class below).        return PlaceholderFragment.newInstance(position + 1);    }//該方法的傳回值i表明該Adapter總共包括多少個Fragment    @Override    public int getCount() {        // Show 3 total pages.        return 3;    }//該方法的傳回值決定每個Fragment的標題    @Override    public CharSequence getPageTitle(int position) {        Locale l = Locale.getDefault();        switch (position) {            case 0:                return getString(R.string.title_section1).toUpperCase(l);            case 1:                return getString(R.string.title_section2).toUpperCase(l);            case 2:                return getString(R.string.title_section3).toUpperCase(l);        }        return null;    }}

TabHost實現導航,分兩種:

使用Tab標籤頁的一般步驟
首先要設計所有的分頁的介面布局
Activity繼承TabActivity
調用TabActivity的getTabHost()方法獲得TabHost對象
通過TabHost建立Tab


TabHost:標籤控制項核心類,標籤的集合
TabHost.TabSpec:標籤對象,可以裝載View視圖。如一個控制項或布局

代碼說明:

//聲明TabHost,然後用LayoutInflater過濾出布局來,給TabHost加上含有Tab頁面的FrameLayoutTabHost myTabhost=this.getTabHost();//從TabActivity上面擷取放置Tab的TabHostLayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);//from(this)從這個TabActivity擷取LayoutInflater//R.layout.main 存放Tab布局  //通過TabHost獲得存放Tab標籤頁內容的FrameLayout  //是否將inflate 拴繫到根布局元素上在TabHost建立一個標籤,然後設定一下標題/表徵圖/標籤頁布局  myTabhost.addTab(myTabhost.newTabSpec("TT")// 造一個新標籤TT        .setIndicator("KK",getResources().getDrawable(R.drawable.ajjc))// 設定一下顯示的標題為KK,設定一下標籤表徵圖為ajjc            .setContent(R.id.widget_layout_red));  //設定一下該標籤頁的布局內容為R.id.widget_layout_red,這是FrameLayout中的一個子Layout

NavigationDrawer實現Tab導航:

//extends Fragment實現對應導航的回調方法public void onNavigationDrawerItemSelected(int position) implements NavigationDrawerFragment.NavigationDrawerCallbacks  mNavigationDrawerFragment = (NavigationDrawerFragment)                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);        mTitle = getTitle();        // Set up the drawer.        mNavigationDrawerFragment.setUp(                R.id.navigation_drawer,                (DrawerLayout) findViewById(R.id.drawer_layout));         //返回對應的Fragment對象                @Override    public void onNavigationDrawerItemSelected(int position) {        // update the main content by replacing fragments        FragmentManager fragmentManager = getSupportFragmentManager();        fragmentManager.beginTransaction()                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))                .commit();    }        //該方法的傳回值決定每個Fragment的標題    public void onSectionAttached(int number) {        switch (number) {            case 1:                mTitle = getString(R.string.title_section1);                break;            case 2:                mTitle = getString(R.string.title_section2);                break;            case 3:                mTitle = getString(R.string.title_section3);                break;        }    }               //設定導航開啟時的導航文字顯示public void restoreActionBar() {        ActionBar actionBar = getSupportActionBar();        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);        actionBar.setDisplayShowTitleEnabled(true);        actionBar.setTitle(mTitle);    }

ScrollableTab實現Tab導航:

// Create the adapter that will return a fragment for each of the three// primary sections of the app.mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());// Set up the ViewPager with the sections adapter.mViewPager = (ViewPager) findViewById(R.id.pager);mViewPager.setAdapter(mSectionsPagerAdapter);public class SectionsPagerAdapter extends FragmentPagerAdapter {public SectionsPagerAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int position) {// getItem is called to instantiate the fragment for the given page.// Return a DummySectionFragment (defined as a static inner class// below) with the page number as its lone argument.Fragment fragment = new DummySectionFragment();Bundle args = new Bundle();args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);fragment.setArguments(args);return fragment;}@Overridepublic int getCount() {// Show 3 total pages.return 3;}@Overridepublic CharSequence getPageTitle(int position) {Locale l = Locale.getDefault();switch (position) {case 0:return getString(R.string.title_section1).toUpperCase(l);case 1:return getString(R.string.title_section2).toUpperCase(l);case 2:return getString(R.string.title_section3).toUpperCase(l);}return null;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.