Android 导航类型

下拉式导航:

    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<String>(//为导航设置列表项
                        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,如下所示
                          
		//当导航被选中时激发该方法
		@Override
		public 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页面的FrameLayout
	TabHost myTabhost=this.getTabHost();
	//从TabActivity上面获取放置Tab的TabHost
	LayoutInflater.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);
		}

		@Override
		public 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;
		}

		@Override
		public int getCount() {
			// Show 3 total pages.
			return 3;
		}

		@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;
		}


Android 导航类型,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。