使用ActionBar Tab(地址)

本文实现将页面分为多个选项卡,并在每一个选项卡中显示一个ListView。

创建新Layout - ActionbarTab.axml, 并向页面中添加FrameLayout控件。 页面源代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:minWidth="25px"
  7. android:minHeight="25px">
  8. <FrameLayout
  9. android:minWidth="25px"
  10. android:minHeight="25px"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:id="@+id/frameLayout1" />
  14. </LinearLayout>

创建新Layout - ListViewLayout.axml, 这个Layout用于定义嵌在Frame - frameLayout1中的ListView控件。 在页面中添加ListView控件 - Id是myList。页面源代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:minWidth="25px"
  7. android:minHeight="25px">
  8. <ListView
  9. android:minWidth="25px"
  10. android:minHeight="25px"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:id="@+id/myList" />
  14. </LinearLayout>

向项目中添加新Activity - ActionbarTabActivity.cs, 在Activity中实现新Tab的创建。

  1. Oncreate()方法中,关联前端UI.

    1. SetContentView(Resource.Layout.ActionbarTab);
  2. 设置页面导航模式为Tabs.

    1. this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
  3. 定义新AddTab方法, 将新创建的Fragment View添加到页面控件frameLayout1中。

    1. void AddTab(string tabText, int iconResourceId, Fragment view)
    2. {
    3. var tab = this.ActionBar.NewTab();
    4. tab.SetText(tabText);
    5. tab.SetIcon(iconResourceId);
    6. tab.TabSelected += delegate (object sender, ActionBar.TabEventArgs e)
    7. {
    8. var fragment = this.FragmentManager.FindFragmentById(Resource.Id.frameLayout1);
    9. if (fragment != null)
    10. e.FragmentTransaction.Remove(fragment);
    11. e.FragmentTransaction.Add(Resource.Id.frameLayout1, view);
    12. };
    13. tab.TabUnselected += delegate (object sender, ActionBar.TabEventArgs e)
    14. {
    15. e.FragmentTransaction.Remove(view);
    16. };
    17. this.ActionBar.AddTab(tab);
    18. }
  4. 定义两个新Fragment类(此处仅demo一个),继承基类Fragment。Fragment类似于子Activity, 这两个Fragment的作用是操作两个Tab页面中的控件。

    1. public class firstFragment:Fragment
    2. {
    3. List<string> Sources = null;
    4. public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    5. {
    6. base.OnCreateView(inflater, container, savedInstanceState);
    7. var view = inflater.Inflate(Resource.Layout.ListViewlayout, container, false);
    8. ListView myList = view.FindViewById<ListView>(Resource.Id.myList);
    9. Sources = new List<string>();
    10. for(int i = 1; i<=10;i++)
    11. {
    12. Sources.Add(string.Format(@"item {0}", i));
    13. }
    14. ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity,
    15. global::Android.Resource.Layout.SimpleListItem1, Sources);
    16. myList.SetAdapter(adapter);
    17. myList.ItemClick += ListClick;
    18. }
    19. void ListClick(object sender, AdapterView.ItemClickEventArgs e)
    20. {
    21. ListView list = sender as ListView;
    22. string selectedFromList = list.GetItemAtPosition(e.Position).ToString();
    23. }
    24. }

    以上代码中需要注意, ArrayAdapter的第一参数在Fragment中是this.Activity, 而在Activity中是thisFindViewById()修改为view.FindViewById()

  5. 添加新Tab.

    1. AddTab("list1", Resource.Drawable.Icon, new firstFragment());

ActionbarTabActivity 源代码如下:

  1. [Activity(Label = "ActionbarTabActivity")]
  2. public class ActionbarTabActivity : Activity
  3. {
  4. protected override void OnCreate(Bundle bundle)
  5. {
  6. base.OnCreate(bundle);
  7. // Create your application here
  8. SetContentView(Resource.Layout.ActionbarTab);
  9. this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
  10. AddTab("list1", Resource.Drawable.Icon, new firstFragment());
  11. if(bundle!=null)
  12. this.ActionBar.SelectTab(this.ActionBar.GetTabAt(bundle.GetInt("tab")));
  13. }
  14. protected override void OnSaveInstanceState(Bundle outState)
  15. {
  16. outState.PutInt("tab", this.ActionBar.SelectedNavigationIndex);
  17. base.OnSaveInstanceState(outState);
  18. }
  19. void AddTab(string tabText, int iconResourceId, Fragment view)
  20. {
  21. var tab = this.ActionBar.NewTab();
  22. tab.SetText(tabText);
  23. tab.SetIcon(iconResourceId);
  24. tab.TabSelected += delegate (object sender, ActionBar.TabEventArgs e)
  25. {
  26. var fragment = this.FragmentManager.FindFragmentById(Resource.Id.frameLayout1);
  27. if (fragment != null)
  28. e.FragmentTransaction.Remove(fragment);
  29. e.FragmentTransaction.Add(Resource.Id.frameLayout1, view);
  30. };
  31. tab.TabUnselected += delegate (object sender, ActionBar.TabEventArgs e)
  32. {
  33. e.FragmentTransaction.Remove(view);
  34. };
  35. this.ActionBar.AddTab(tab);
  36. }
  37. public class firstFragment:Fragment
  38. {
  39. List<string> Sources = null;
  40. public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  41. {
  42. base.OnCreateView(inflater, container, savedInstanceState);
  43. var view = inflater.Inflate(Resource.Layout.ListViewlayout, container, false);
  44. ListView myList = view.FindViewById<ListView>(Resource.Id.myList);
  45. Sources = new List<string>();
  46. for(int i = 1; i<=10;i++)
  47. {
  48. Sources.Add(string.Format(@"item {0}", i));
  49. }
  50. ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity,
  51. global::Android.Resource.Layout.SimpleListItem1, Sources);
  52. myList.SetAdapter(adapter);
  53. myList.ItemClick += ListClick;
  54. return view;
  55. }
  56. void ListClick(object sender, AdapterView.ItemClickEventArgs e)
  57. {
  58. ListView list = sender as ListView;
  59. string selectedFromList = list.GetItemAtPosition(e.Position).ToString();
  60. }
  61. }
  62. }

使用ActionBar Tab的更多相关文章

  1. Android tab导航的几种方法:ActionBar tab +fragment,Viewpager+pagerTitleStrip,开源框架ViewPageIndicator 和 ViewPager

    action来实现tab标签 并跟fragment结合 因为要写新闻客户端这个tab导航是必须的 这里我写几个小练习,希望大家融会贯通. 1actionbar设置tab +fragment 布局是个l ...

  2. ActionBar +Tab+ViewPager +Fragment 支持侧滑动完成办税工具的页面展示

    1:fragment_zhqrl.xml(征期日历) <?xml version="1.0" encoding="utf-8"?> <Line ...

  3. 15 ActionBar.Tab 以及保存fragment对象 代码案例

    API 21弃用 values 中 string文件源码: <?xml version="1.0" encoding="utf-8"?> <r ...

  4. Android 原生 Android ActionBar Tab (滑动)导航

    本文内容 环境 项目结构 演示一:ActionBar Tab 导航 演示二:ActionBar Tab 带滑动导航 本文演示 Tab 导航.第一个演示,是基本的 Tab 导航,第二个是带滑动的 Tab ...

  5. Android Actionbar Tab 导航模式

    Android Actionbar Tab 下图中,红色矩形圈起来的就是我们 ActionBar Tab,下面我们将一步一步的实现下图中的效果. 初次尝试 package com.example.it ...

  6. Android典型界面设计(6)——ActionBar Tab+ViewPager+Fagment实现滑动导航

    一.问题描述 在Android典型界面设计一文中,实现典型滑动导航界面,其实使用ActionBar 也可以轻松实现这一效果,甚至也可实现类似Android典型界面设计(3)的双导航效果.可见Actio ...

  7. actionbar tab 字体大小设置

    在styles.xml文件里加入以下的样式就可以 <!-- Application theme. -->     <style name="AppTheme" p ...

  8. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  9. Android ActionBar 关于tab的应用 以及 TabListener的方法详解

    actionBar的tab标签应用以及TabListener的方法详解 package com.example.actionBarTest.actionBarTab; import android.a ...

随机推荐

  1. 程设大作业xjb写——魔方复原

    鸽了那么久总算期中过[爆]去[炸]了...该是时候写写大作业了 [总不能丢给他们不会写的来做吧 一.三阶魔方的几个基本定义 ↑就像这样,可以定义面的称呼:上U下D左L右R前F后B UD之间的叫E,LR ...

  2. C宏展开的几个注意事项

    前阵子仔细重新研究了一下C的宏展开.总结起来,有以下几个主要规则: 每次宏展开的结果会被重复扫描,直到没有任何可展开的宏为止. 每展开一个宏,都会记住这次展开,在这个宏展开的结果及其后续展开中,不再对 ...

  3. Shell study note

    td p { margin-bottom: 0in } p { margin-bottom: 0.1in; line-height: 120% } a:link { } 5.1 printenv vi ...

  4. nodejs 转发websocket (websocket proxy)

    const http = require('http') const server = http.createServer((req, res) =>{ res.end('hello world ...

  5. sql 2008 修改链接服务器 Rpc &Rpc Out

    From: http://blog.csdn.net/gnolhh168/article/details/41725873 USE [master] GO EXEC master.dbo.sp_ser ...

  6. robots笔记以免忘记

    html头部标签写法: <meta name="robots" content="index,follow" /> content中的值决定允许抓取 ...

  7. 腾讯云服务器centos 6.5(jdk+tomcat+vsftp)、腾讯mysql数据库 及 tomcat自启动 配置教程

    1.腾讯云数据库配置 1.考虑到安全性问题,,平常不使用root用户登录,新增一个用户名neil,用来管理项目的数据库 a.首先登录root创建db_AA数据库 b.在root用户下,创建neil用户 ...

  8. Work around by " Due to heavy load, the latest workflow operation has been queued. " 分类: Sharepoint 2015-07-08 00:19 3人阅读 评论(0) 收藏

    I hope most of the users and developers might have come across above note and worried about it. Ther ...

  9. asp.net错误页和asp.net mvc错误页设置

    asp.net错误页 在日常项目开发过程中,我们需要给网站设置错误页和记录错误日志. 首先,在项目中添加全局应用程序类 在Global.asax中 protected void Application ...

  10. SQL--create Table

    use MiddleHospitalgocreate table CMS_Infopublish_Auction( AuctionID int identity(1, 1) primary key,  ...